package com.qujia.station.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.core.domain.entity.SysRole;
import com.qujia.common.core.domain.entity.SysUser;
import com.qujia.common.core.domain.model.LoginUser;
import com.qujia.common.utils.DateUtils;
import com.qujia.common.utils.MathUtil;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.common.utils.StringUtils;
import com.qujia.customer.enums.CardRecordType;
import com.qujia.station.domain.*;
import com.qujia.station.domain.dto.CardClassSettDto;
import com.qujia.station.domain.dto.OilEngineDto;
import com.qujia.station.domain.dto.OilEngineSumDto;
import com.qujia.station.domain.dto.StationClassTimeDto;
import com.qujia.station.domain.vo.StationClassTimeVo;
import com.qujia.station.domain.vo.changeClassSettlement4User;
import com.qujia.station.mapper.*;
import com.qujia.station.service.ClassSettlementCardService;
import com.qujia.station.utils.StaffUtil;
import com.qujia.system.mapper.SysUserMapper;
import com.qujia.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.qujia.station.service.IClassSettlementService;

import javax.annotation.Resource;

/**
 * 班结Service业务层处理
 *
 * @author qujia
 * @date 2021-11-27
 */
@Service
public class ClassSettlementServiceImpl implements IClassSettlementService {
    @Autowired
    private ClassSettlementMapper classSettlementMapper;
    @Resource
    private OilOmeterMapper oilOmeterMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private ClassSettlementCardMapper classSettlementCardMapper;
    @Autowired
    private ClassSettlementCardService classSettlementCardService;
    @Resource
    private ISysUserService sysUserService;
    @Autowired
    private StationMapper stationMapper;
    @Autowired
    private EnterpriseMapper enterpriseMapper;

    /**
     * 查询班结
     *
     * @param classSettlementId 班结ID
     * @return 班结
     */
    @Override
    public ClassSettlement selectClassSettlementById(Long classSettlementId) {
        return classSettlementMapper.selectClassSettlementById(classSettlementId);
    }

    /**
     * 查询班结列表
     *
     * @param classSettlement 班结
     * @return 班结
     */
    @Override
    public List<ClassSettlement> selectClassSettlementList(ClassSettlement classSettlement) {
        return classSettlementMapper.selectClassSettlementList(classSettlement);
    }

    /**
     * 查询班结汇总列表
     *
     * @param classSettlement 班结
     * @return 班结
     */
    @Override
    public List<ClassSettlement> selectClassSettlementSummaryList(ClassSettlement classSettlement) {

            return classSettlementMapper.selectClassSettlementSummaryAllList(classSettlement);


    }

    @Override
    public List<ClassSettlement> selectClassSettlementStaffSummaryList(ClassSettlement classSettlement) {
        return classSettlementMapper.selectClassSettlementStaffSummaryList(classSettlement);
    }

    @Override
    public List<ClassSettlement> classTimeList(ClassSettlement classSettlement) {
        return classSettlementMapper.classTimeList(classSettlement);
    }

    @Override
    public List<BusinessDailyReport> selectBusinessDailyReportList(ClassSettlement classSettlement) {
        if( classSettlement.getCheckTime() == 1){
            return classSettlementMapper.selectClassSettlementSummaryListInReportEight(classSettlement);
        }else {
            return classSettlementMapper.selectBusinessDailyReportList(classSettlement);
        }
    }

    @Override
    public List<BusinessDailyReport> selectClassSettlementSummaryListInReport(ClassSettlement classSettlement) {
        return classSettlementMapper.selectClassSettlementSummaryListInReport(classSettlement);
    }

    @Override
    public BusinessDailyReport selectBusinessDailyReportSum(ClassSettlement classSettlement) {
        if( classSettlement.getCheckTime() == 1){
            return classSettlementMapper.selectBusinessDailyReportSumInReportEight(classSettlement);
        }else {
            return classSettlementMapper.selectBusinessDailyReportSum(classSettlement);
        }
    }

    @Override
    public BusinessDailyReport selectBusinessDailyReportSumInReport(ClassSettlement classSettlement) {
        return classSettlementMapper.selectBusinessDailyReportSumInReport(classSettlement);
    }

    @Override
    public List<ReportSettlement> selectReportClassList(ClassSettlement classSettlement) {
        LoginUser current = SecurityUtils.getLoginUser();
        if ("1".equals(current.getUser().getUserLevel())) {
            Station station = new Station();
            station.setEnterpriseId(current.getUser().getEnterpriseId());
            List<String> list = new ArrayList<>();
            List<Station> stations = stationMapper.selectStationList(station);

            if(stations==null||stations.size()==0){
                List<ReportSettlement> payOrderList = new ArrayList<>();
                return payOrderList;
            }
            for (Station ss : stations) {
                list.add(ss.getStationId() + "");
            }
            if (stations != null && stations.size() > 0) {
                classSettlement.setStationIdList(list);
            }

        } else if ("2".equals(current.getUser().getUserLevel())) {
            classSettlement.setStationId(current.getUser().getStationId());
        } else {

        }
        List<ReportSettlement> reportClass = classSettlementMapper.selectReportClassList(classSettlement);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(ReportSettlement reportSettlement:reportClass){
            if (reportSettlement.getClassStartTime() != null) {

                Date startTime = reportSettlement.getClassStartTime();
                String formattedStartTime = sdf.format(startTime);
                classSettlement.setBeginTime(formattedStartTime);
            } else {
                classSettlement.setBeginTime(null);
            }

            if (reportSettlement.getClassEndTime() != null) {
                Date endTime = reportSettlement.getClassEndTime();
                String formattedEndTime = sdf.format(endTime);
                classSettlement.setEndTime(formattedEndTime);
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String formattedDate = dateFormat.format(endTime);
                reportSettlement.setTimeOnTrade(formattedDate);
            } else {
                classSettlement.setEndTime(null);
            }
            classSettlement.setStationId(reportSettlement.getStationId());
            classSettlement.setTimeOnTrade(reportSettlement.getTimeOnTrade());
            BigDecimal a =   classSettlementMapper.totalWordCount(classSettlement);
            if(a != null){
                reportSettlement.setTotalWordCount(a);
            }
        }
        return reportClass;
    }

    @Override
    public List<ClassSettlementCard> getTransactionSummary(CardClassSettDto settDto, Map<String, Object> params) {
        List<ClassSettlementCard> transactionSummary = new ArrayList<>();
        List<ClassSettlementCard> settCardList = settDto.getSettlementCardList();
        ClassSettlementCard wxrefClassSettlementCard = new ClassSettlementCard();
        ClassSettlementCard posrefClassSettlementCard = new ClassSettlementCard();
        for (ClassSettlementCard settCard : settCardList) {
            if (settCard.getPayType() != null && settCard.getPayType().equals(CardRecordType.WX_REFUND.getKey())) {
                wxrefClassSettlementCard = settCard;
            }
            if (settCard.getPayType() != null && settCard.getPayType().equals(CardRecordType.POS_REFUND.getKey())) {
                posrefClassSettlementCard = settCard;
            }
        }
        for (ClassSettlementCard settCard : settCardList) {
            if (settCard.getPayType() != null && settCard.getPayType().equals("imp")) {
                transactionSummary.add(settCard);
            }else if(settCard.getPayType() != null && settCard.getPayType().equals("pos")) {
                settCard.setAmt(settCard.getAmt().subtract(posrefClassSettlementCard.getAmt()== null ? BigDecimal.ZERO : posrefClassSettlementCard.getAmt()));
                settCard.setGiveAmt(settCard.getGiveAmt().subtract(posrefClassSettlementCard.getGiveAmt()== null ? BigDecimal.ZERO : posrefClassSettlementCard.getGiveAmt()));
                if (settCard.getOrderCount() != null && posrefClassSettlementCard.getOrderCount() != null) {
                    settCard.setOrderCount(settCard.getOrderCount() - posrefClassSettlementCard.getOrderCount());
                }
                transactionSummary.add(settCard);
            }
            else if (settCard.getPayType() != null && settCard.getPayType().equals("wx")) {
                settCard.setAmt(settCard.getAmt().subtract(wxrefClassSettlementCard.getAmt()== null ? BigDecimal.ZERO : wxrefClassSettlementCard.getAmt()));
                settCard.setGiveAmt(settCard.getGiveAmt().subtract(wxrefClassSettlementCard.getGiveAmt()== null ? BigDecimal.ZERO : wxrefClassSettlementCard.getGiveAmt()));
                if (settCard.getOrderCount() != null && wxrefClassSettlementCard.getOrderCount() != null) {
                    settCard.setOrderCount(settCard.getOrderCount() - wxrefClassSettlementCard.getOrderCount());
                }
                transactionSummary.add(settCard);
            }
        }
        ClassSettlementCard physicalCardInXj = classSettlementMapper.queryPhysicalCardInXj(params);
        physicalCardInXj.setPayType("dkqxj");
        ClassSettlementCard physicalCardInWxPos = classSettlementMapper.queryPhysicalCardInWxPos(params);
        physicalCardInWxPos.setPayType("dkqwxpos");
        ClassSettlementCard physicalCardInRef = classSettlementMapper.queryPhysicalCardInRef(params);
        physicalCardInRef.setPayType("dkqref");
        if (physicalCardInWxPos.getAmt()!=null && physicalCardInRef.getAmt()!=null){
            BigDecimal wxpos =   physicalCardInWxPos.getAmt().subtract(physicalCardInRef.getAmt());
            BigDecimal wxposgift =   BigDecimal.ZERO;
            long wxposcount =   physicalCardInWxPos.getOrderCount()-physicalCardInRef.getOrderCount();
            physicalCardInWxPos.setAmt(wxpos);
            physicalCardInWxPos.setGiveAmt(wxposgift);
            physicalCardInWxPos.setOrderCount(wxposcount);
        }
        ClassSettlementCard physicalCard = classSettlementMapper.queryPhysicalCard(params);
        physicalCard.setPayType("dkq");
        ClassSettlementCard fleetCard = classSettlementMapper.queryFleetCard(params);
        fleetCard.setPayType("cdk");
        ClassSettlementCard SuperPay = classSettlementMapper.querySuperPay(params);
        SuperPay.setPayType("cjhyxs");
        ClassSettlementCard cxhysd =new ClassSettlementCard();
        cxhysd.setPayType("cjhysd");
        transactionSummary.add(physicalCardInXj);
        transactionSummary.add(physicalCardInWxPos);
        transactionSummary.add(fleetCard);
        transactionSummary.add(SuperPay);
        transactionSummary.add(cxhysd);
        ClassSettlementCard alltransaction = new ClassSettlementCard();
        alltransaction.setPayType("合计");
        BigDecimal totalAmt = BigDecimal.ZERO;
        BigDecimal totalWithdrawalAmount = BigDecimal.ZERO;
        BigDecimal giveAmt = BigDecimal.ZERO;

        Long totalOrderCount = 0L;

        for (ClassSettlementCard transaction : transactionSummary) {
            if (transaction.getAmt() != null) {
                totalAmt = totalAmt.add(transaction.getAmt());
            }
            if (transaction.getWithdrawalAmount() != null) {
                totalWithdrawalAmount = totalWithdrawalAmount.add(transaction.getWithdrawalAmount());
            }
            if (transaction.getGiveAmt() != null) {
                giveAmt = giveAmt.add(transaction.getGiveAmt());
            }
            if (transaction.getOrderCount() != null) {
                totalOrderCount = totalOrderCount + transaction.getOrderCount();
            }
        }

        alltransaction.setAmt(totalAmt);
        alltransaction.setGiveAmt(giveAmt);
        alltransaction.setWithdrawalAmount(totalWithdrawalAmount);
        alltransaction.setOrderCount(totalOrderCount);
        transactionSummary.add(alltransaction);
        return transactionSummary;
    }



    @Override
    public List<ClassSettlementCard> getStaffTransactionSummary(CardClassSettDto settDto, Map<String, Object> params) {
        List<ClassSettlementCard> transactionSummary = new ArrayList<>();
        List<ClassSettlementCard> settCardList = settDto.getSettlementCardList();
        ClassSettlementCard wxrefClassSettlementCard = new ClassSettlementCard();
        ClassSettlementCard posrefClassSettlementCard = new ClassSettlementCard();
        for (ClassSettlementCard settCard : settCardList) {
            if (settCard.getPayType() != null && settCard.getPayType().equals(CardRecordType.WX_REFUND.getKey())) {
                wxrefClassSettlementCard = settCard;
            }
            if (settCard.getPayType() != null && settCard.getPayType().equals(CardRecordType.POS_REFUND.getKey())) {
                posrefClassSettlementCard = settCard;
            }
        }
        for (ClassSettlementCard settCard : settCardList) {
            if (settCard.getPayType() != null && settCard.getPayType().equals("imp")) {
                transactionSummary.add(settCard);
            }else if(settCard.getPayType() != null && settCard.getPayType().equals("pos")) {
                settCard.setAmt(settCard.getAmt().subtract(posrefClassSettlementCard.getAmt()== null ? BigDecimal.ZERO : posrefClassSettlementCard.getAmt()));
                settCard.setGiveAmt(settCard.getGiveAmt().subtract(posrefClassSettlementCard.getGiveAmt()== null ? BigDecimal.ZERO : posrefClassSettlementCard.getGiveAmt()));
                if (settCard.getOrderCount() != null && posrefClassSettlementCard.getOrderCount() != null) {
                    settCard.setOrderCount(settCard.getOrderCount() - posrefClassSettlementCard.getOrderCount());
                }
                transactionSummary.add(settCard);
            }
            else if (settCard.getPayType() != null && settCard.getPayType().equals("wx")) {
                settCard.setAmt(settCard.getAmt().subtract(wxrefClassSettlementCard.getAmt()== null ? BigDecimal.ZERO : wxrefClassSettlementCard.getAmt()));
                settCard.setGiveAmt(settCard.getGiveAmt().subtract(wxrefClassSettlementCard.getGiveAmt()== null ? BigDecimal.ZERO : wxrefClassSettlementCard.getGiveAmt()));
                if (settCard.getOrderCount() != null && wxrefClassSettlementCard.getOrderCount() != null) {
                    settCard.setOrderCount(settCard.getOrderCount() - wxrefClassSettlementCard.getOrderCount());
                }
                transactionSummary.add(settCard);
            }
        }
        ClassSettlementCard alltransaction = new ClassSettlementCard();
        alltransaction.setPayType("合计");
        BigDecimal totalAmt = BigDecimal.ZERO;
        BigDecimal totalWithdrawalAmount = BigDecimal.ZERO;
        BigDecimal giveAmt = BigDecimal.ZERO;

        Long totalOrderCount = 0L;

        for (ClassSettlementCard transaction : transactionSummary) {
            if (transaction.getAmt() != null) {
                totalAmt = totalAmt.add(transaction.getAmt());
            }
            if (transaction.getWithdrawalAmount() != null) {
                totalWithdrawalAmount = totalWithdrawalAmount.add(transaction.getWithdrawalAmount());
            }
            if (transaction.getGiveAmt() != null) {
                giveAmt = giveAmt.add(transaction.getGiveAmt());
            }
            if (transaction.getOrderCount() != null) {
                totalOrderCount = totalOrderCount + transaction.getOrderCount();
            }
        }

        alltransaction.setAmt(totalAmt);
        alltransaction.setGiveAmt(giveAmt);
        alltransaction.setWithdrawalAmount(totalWithdrawalAmount);
        alltransaction.setOrderCount(totalOrderCount);
        transactionSummary.add(alltransaction);
        return transactionSummary;
    }

    @Override
    public List<ClassSettlement> getRefundSummary(CardClassSettDto settDto, Map<String, Object> params) {
        Boolean notisStaff  = true;
        if(params.get("notisStaff")!=null){
            notisStaff = (Boolean)params.get("notisStaff");
        }
if(notisStaff) {
    List<ClassSettlement> refundSummary = new ArrayList<>();
    List<ClassSettlementCard> settCardList = settDto.getSettlementCardList();
    ClassSettlement refSettlement = new ClassSettlement();
    for (ClassSettlementCard settCard : settCardList) {
        if (settCard.getPayType() != null && settCard.getPayType().equals("ref")) {
            if (settCard.getOrderCount() != null && settCard.getOrderCount() != 0L) {
                refSettlement.setPayType("ref");
                refSettlement.setRefundAmt(settCard.getAmt());
                refSettlement.setOrderCount(settCard.getOrderCount());
                refundSummary.add(refSettlement);
            }
        }
    }
    ClassSettlementCard refSaleActivity = classSettlementMapper.refSaleActivityAmt(params);
    ClassSettlement msSettlement = new ClassSettlement();
    msSettlement.setPayType("msref");
    msSettlement.setRefundAmt(refSaleActivity.getAmt() != null ? refSaleActivity.getAmt() : BigDecimal.ZERO);
    msSettlement.setOrderCount(refSaleActivity.getOrderCount());
    refundSummary.add(msSettlement);
    // 根据油品分组查询统计信息
    List<Map<String, Object>> queryGroupByPayType = classSettlementMapper.queryGroupRefundByPayTypeForOrder(params);
    Iterator<Map<String, Object>> iterator3 = queryGroupByPayType.iterator();
    ClassSettlementCard refundSuperPay = classSettlementMapper.queryRefundSuperPay(params);
    ClassSettlementCard physicalCardInRef = classSettlementMapper.queryPhysicalCardInRef(params);

    double refundSuperPayAmt = refundSuperPay.getAmt() != null ? refundSuperPay.getAmt().doubleValue() : 0.0;
    BigDecimal refundSuperPayCount = refundSuperPay.getOrderCount() != null ? new BigDecimal(refundSuperPay.getOrderCount()) : BigDecimal.ZERO;
    double physicalCardInRefAmt = physicalCardInRef.getAmt() != null ? physicalCardInRef.getAmt().doubleValue() : 0.0;
    BigDecimal physicalCardInRefCount = physicalCardInRef.getOrderCount() != null ? new BigDecimal(physicalCardInRef.getOrderCount()) : BigDecimal.ZERO;

    while (iterator3.hasNext()) {
        Map<String, Object> map = iterator3.next();
        if (!map.containsKey("pay_type")) {
            iterator3.remove();
        }
    }
    Map<String, Object> itemPayTypeMap = new HashMap<>();
    itemPayTypeMap.put("pay_type", "sref");
    itemPayTypeMap.put("refund_amt", refundSuperPayAmt);
    itemPayTypeMap.put("order_count", refundSuperPayCount);
    queryGroupByPayType.add(itemPayTypeMap);
    Map<String, Object> itemPayTypeSTKMap = new HashMap<>();
    itemPayTypeSTKMap.put("pay_type", "dkqwxposref");
    itemPayTypeSTKMap.put("refund_amt", physicalCardInRefAmt);
    itemPayTypeSTKMap.put("order_count", physicalCardInRefCount);
    queryGroupByPayType.add(itemPayTypeSTKMap);
    for (Map<String, Object> itemPayType : queryGroupByPayType) {
        ClassSettlement refunditem = new ClassSettlement();
        refunditem.setPayType(itemPayType.get("pay_type").toString());
        Object refundAmtObj = itemPayType.get("refund_amt");
        double refundAmtDouble = (refundAmtObj != null) ? (double) refundAmtObj : 0.0;
        BigDecimal refundAmt = BigDecimal.valueOf(refundAmtDouble);
        if (refundAmt != null) {
            refunditem.setRefundAmt(refundAmt);
        }
        Object countObj = itemPayType.get("order_count");
        Long count = (countObj != null) ? ((BigDecimal) countObj).longValue() : 0L;
        if (count != null) {
            refunditem.setOrderCount(count);
        }
        refundSummary.add(refunditem);
    }
    ClassSettlement allRefund = new ClassSettlement();
    allRefund.setPayType("合计");
    BigDecimal allRefundAmt = BigDecimal.ZERO;
    Long totalOrderCount = 0L;
    for (ClassSettlement refunditem : refundSummary) {
        if (refunditem.getRefundAmt() != null) {
            allRefundAmt = allRefundAmt.add(refunditem.getRefundAmt());
        }

        if (refunditem.getOrderCount() != null) {
            totalOrderCount = totalOrderCount + refunditem.getOrderCount();
        }
    }
    allRefund.setRefundAmt(allRefundAmt);
    allRefund.setOrderCount(totalOrderCount);
    refundSummary.add(allRefund);
    return refundSummary;
}else{

    List<ClassSettlement> refundSummary = new ArrayList<>();
    List<Map<String, Object>> queryGroupByPayType = classSettlementMapper.queryGroupRefundByPayTypeForOrder(params);
    for (Map<String, Object> itemPayType : queryGroupByPayType) {
        ClassSettlement refunditem = new ClassSettlement();
        refunditem.setPayType(itemPayType.get("pay_type").toString());
        Object refundAmtObj = itemPayType.get("refund_amt");
        double refundAmtDouble = (refundAmtObj != null) ? (double) refundAmtObj : 0.0;
        BigDecimal refundAmt = BigDecimal.valueOf(refundAmtDouble);
        if (refundAmt != null) {
            refunditem.setRefundAmt(refundAmt);
        }
        Object countObj = itemPayType.get("order_count");
        Long count = (countObj != null) ? ((BigDecimal) countObj).longValue() : 0L;
        if (count != null) {
            refunditem.setOrderCount(count);
        }
        refundSummary.add(refunditem);
    }
    ClassSettlement allRefund = new ClassSettlement();
    allRefund.setPayType("合计");
    BigDecimal allRefundAmt = BigDecimal.ZERO;
    Long totalOrderCount = 0L;
    for (ClassSettlement refunditem : refundSummary) {
        if (refunditem.getRefundAmt() != null) {
            allRefundAmt = allRefundAmt.add(refunditem.getRefundAmt());
        }

        if (refunditem.getOrderCount() != null) {
            totalOrderCount = totalOrderCount + refunditem.getOrderCount();
        }
    }
    allRefund.setRefundAmt(allRefundAmt);
    allRefund.setOrderCount(totalOrderCount);
    refundSummary.add(allRefund);
    return refundSummary;
   }
    }

    @Override
    public List<ClassSettlement> employeeGetRefundSummary(Map<String, Object> params) {
        List<ClassSettlement> refundSummary = new ArrayList<>();

        // 员工权限下不统计电子卡（会员卡）充值退款
//        List<ClassSettlementCard> settCardList = settDto.getSettlementCardList();
//        ClassSettlement refSettlement = new ClassSettlement();
//        for (ClassSettlementCard settCard : settCardList) {
//            if (settCard.getPayType() != null && settCard.getPayType().equals("ref")) {
//                if (settCard.getOrderCount() != null && settCard.getOrderCount() != 0L) {
//                    refSettlement.setPayType("ref");
//                    refSettlement.setRefundAmt(settCard.getAmt());
//                    refSettlement.setOrderCount(settCard.getOrderCount());
//                    refundSummary.add(refSettlement);
//                }
//            }
//        }

        // 根据员工+支付方式分组查询统计信息
        List<Map<String, Object>> queryGroupByPayType = classSettlementMapper.employeeQueryGroupRefundByPayTypeForOrder(params);
        Iterator<Map<String, Object>> iterator3 = queryGroupByPayType.iterator();
        while (iterator3.hasNext()) {
            Map<String, Object> map = iterator3.next();
            if (!map.containsKey("pay_type")) {
                iterator3.remove();
            }
        }
        for (Map<String, Object> itemPayType : queryGroupByPayType) {
            ClassSettlement refunditem = new ClassSettlement();
            refunditem.setPayType(itemPayType.get("pay_type").toString());
            Object refundAmtObj = itemPayType.get("refund_amt");
            double refundAmtDouble = (refundAmtObj != null) ? (double) refundAmtObj : 0.0;
            BigDecimal refundAmt = BigDecimal.valueOf(refundAmtDouble);
            if (refundAmt != null) {
                refunditem.setRefundAmt(refundAmt);
            }
            Object countObj = itemPayType.get("order_count");
            Long count = (countObj != null) ? ((BigDecimal) countObj).longValue() : 0L;
            if (count != null) {
                refunditem.setOrderCount(count);
            }
            refundSummary.add(refunditem);
        }
        ClassSettlement allRefund = new ClassSettlement();
        allRefund.setPayType("合计");
        BigDecimal allRefundAmt = BigDecimal.ZERO;
        Long totalOrderCount = 0L;
        for (ClassSettlement refunditem : refundSummary) {
            if (refunditem.getRefundAmt() != null) {
                allRefundAmt = allRefundAmt.add(refunditem.getRefundAmt());
            }

            if (refunditem.getOrderCount() != null) {
                totalOrderCount = totalOrderCount + refunditem.getOrderCount();
            }
        }
        allRefund.setRefundAmt(allRefundAmt);
        allRefund.setOrderCount(totalOrderCount);
        refundSummary.add(allRefund);
        return refundSummary;
    }

    @Override
    public ClassSettlement selectClassSettlementSummary(ClassSettlement classSettlement) {
            return classSettlementMapper.selectClassSettlementAllSummary(classSettlement);
    }

    @Override
    public ClassSettlement selectClassSettlementStaffSummary(ClassSettlement classSettlement) {
        return classSettlementMapper.selectClassSettlementStaffSummary(classSettlement);
    }


    @Override
//    @Async("threadPoolTaskExecutor")
    public ClassSettlementInfo classPushInfoByClassNo(ClassSettlement classSettlement) {
//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            Thread.currentThread().interrupt();
//            // 处理异常逻辑，可以选择抛出自定义异常或记录日志等操作
//        }
        System.out.println();
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String classNo =  classSettlement.getClassNo();
        Map<String, Object> classInfoMap = classInfoByClassNo(classNo);
        ClassSettlementInfo classSettlementInfo = new ClassSettlementInfo();
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("stationId", classSettlement.getStationId());
        queryMap.put("classNo", classSettlement.getClassNo());
        //合计所有油品信息的集合
        List<ClassSettlement> sumOilList = queryClassSettlementOil(queryMap);
        //合计班结信息，按照支付方式的集合
        List<ClassSettlement> sumPayTypeList = queryClassSettlementPayType(queryMap);
        ClassSettlementCard classSettlementCard = new ClassSettlementCard();
        classSettlementCard.setClassNo(classSettlement.getClassNo());
//合计班结信息，充值的信息集合
        List<ClassSettlementCard> sumCzList = classSettlementCardService.queryGroupByCardType(classSettlementCard);
        //加油员班结信息汇总
        List<ClassSettlement> oilPersonList = selectClassSettlementSummaryUser(queryMap);
        //充值金额合计
        ClassSettlementCard classSettlementCardCz = classSettlementCardService.querySumCz(queryMap);

        List<Map<String, Object>> oilPersonClass = new ArrayList<>();
        for (int i = 0; i < oilPersonList.size(); i++) {
            Map<String, Object> person1 = new HashMap<>();
            person1.put("personName", oilPersonList.get(i).getOilPersonnelName());
            if (classSettlement.getClassStartTime() == null) {
                person1.put("classStartTime", "首次");
            } else {
                person1.put("classStartTime", fmt.format(classSettlement.getClassStartTime()));
            }
            person1.put("classEndTime", fmt.format(classSettlement.getClassEndTime()));
            Map<String, Object> queryMap1 = new HashMap<>();
            queryMap1.put("stationId", classSettlement.getStationId());
            queryMap1.put("classNo", classSettlement.getClassNo());
            queryMap1.put("oilPersonnelId", oilPersonList.get(i).getOilPersonnelId());
            //合计所有油品信息的集合
            List<ClassSettlement> OilList = queryClassSettlementOil(queryMap);
            person1.put("oilList", OilList);
            person1.put("orderCount", oilPersonList.get(i).getOrderCount());
            person1.put("receivableAmt", oilPersonList.get(i).getReceivableAmt());
            person1.put("discountAmt", oilPersonList.get(i).getDiscountAmt());
            person1.put("refundAmt", oilPersonList.get(i).getRefundAmt());
            person1.put("amt", oilPersonList.get(i).getAmt());
            person1.put("oilPersonnelName", oilPersonList.get(i).getOilPersonnelName());
            oilPersonClass.add(person1);
        }
        // 其他逻辑
        Map<String, Object> sumClassMap = new HashMap<>();
        sumClassMap.put("sumClassList",classInfoMap.get("querySummaryByClassNo") );
        sumClassMap.put("sumOilList", sumOilList);
        sumClassMap.put("sumPayTypeList", sumPayTypeList);
        sumClassMap.put("sumCzList", sumCzList);
        sumClassMap.put("oilPersonClass", oilPersonClass);

        classSettlementInfo.setSumClassList((List<ClassSettlement>)classInfoMap.get("querySummaryByClassNo"));

        classSettlementInfo.setSumOilList(sumOilList);

        classSettlementInfo.setSumPayTypeList(sumPayTypeList);

        classSettlementInfo.setSumCzList(sumCzList);

        classSettlementInfo.setOilPersonClass(oilPersonClass);

        classSettlementInfo.setClassSettlement(classSettlement);

        classSettlementInfo.setRefundSummary((List<ClassSettlement>)classInfoMap.get("refundSummary"));


        classSettlementInfo.setListDetail4User((List<Map<String, Object>>)classInfoMap.get("listDetail4User"));

        classSettlementInfo.setTransactionSummary((List<ClassSettlementCard>)classInfoMap.get("transactionSummary"));

        classSettlementInfo.setCouponSummary((List<CouponSummary>)classInfoMap.get("couponSummary"));

        classSettlementInfo.setQueryGroupByRecharge((List<ClassSettlementCard>)classInfoMap.get("queryGroupByRecharge"));

        classSettlementInfo.setQueryGroupDiscountByOil((List<Map<String, Object>>)classInfoMap.get("queryGroupDiscountByOil"));

        return classSettlementInfo;
    }

    @Override
    public List<Map<String, Object>> createList( ClassSettlement classSettlement){
        return classSettlementMapper.createList(classSettlement);
    }

    @Override
    public List<Long> selectSysUserByCreateUser( Long createUser){
        return   classSettlementMapper.selectSysUserByCreateUser(createUser);
    }


    @Override
    public Map<String, Object> classInfoByClassNo(String classNo) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("classNo", classNo);

       Long createUser = classSettlementMapper.selectSysUserByNo(queryMap);
        List<Long> roleIds = classSettlementMapper.selectSysUserByCreateUser(createUser);
        SysUser uuser =   userMapper.getById(createUser);
        List<SysRole> roles = new ArrayList<>();
        for(Long roleId:roleIds){
            SysRole a = new SysRole();
            a.setRoleId(roleId);
            roles.add(a);
        }
        System.out.println("classNo" + classNo +"uuser"+ uuser +"roles" +roles);
        uuser.setRoles(roles);

        boolean notisStaff = true;
        String sign = sysUserService.getEmployeeSign(uuser.getUserId());
        // 员工权限
        if(uuser!=null && sign.equals("1")){
            notisStaff = false;
        }
//        if(StaffUtil.isStaff(uuser)){
//
//        }
        // 根据班结编号查询油机信息
        List<ClassSettlementOilmachine> classSettlementOilmachines = selectAllClassSettlementOilmachines(classNo);
        // 根据班结编号查询班结摘要信息
        List<Map<String, Object>> queryGroupByOil = queryGroupByOil(queryMap);
        // 根据油品分组查询统计信息
        List<Map<String, Object>> queryGroupByPayType = queryGroupByPayType(queryMap);

        List<Map<String, Object>> queryGroupDiscountByOil = queryGroupDiscountByOilForOrder(queryMap);


        // 根据充值加油员
        List<changeClassSettlement4User> changeClassSettlement4User = selectForRecharge(queryMap);
        // 根据支付方式分组查询统计信息
        List<Map<String, Object>> queryGroupByOilGun = queryGroupByOilGun(queryMap);
        // 根据油枪号分组查询统计信息
        List<Map<String, Object>> queryGroupByActivity = queryGroupByActivity(queryMap);
        // 根据活动分组查询统计信息
        List<Map<String, Object>> listDetail4User = selectClassSettlement4User4POSList(queryMap);
        ClassSettlementCard form = new ClassSettlementCard();
        form.setClassNo(classNo);
        List<ClassSettlementCard> classSettlementCards = classSettlementCardService.selectClassSettlementCardList(form);
        CardClassSettDto settDto = countCardSett(classSettlementCards); //电子卡汇总数据
        List<ClassSettlement> querySummaryByClassNo = querySummaryByClassNoAllData(queryMap, settDto);
        Map<String, Object> params = new HashMap<String, Object>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createStartTime = querySummaryByClassNo.get(0).getClassStartTime();
        if (createStartTime != null) {
            String classStartTime = dateFormat.format(createStartTime);
            params.put("createStartTime", classStartTime);
        }
        Date createEndTime = querySummaryByClassNo.get(0).getClassEndTime();
        String classEndTime = dateFormat.format(createEndTime);
        params.put("createEndTime", classEndTime);
        params.put("stationId", querySummaryByClassNo.get(0).getStationId());
        params.put("notisStaff",notisStaff);
        //        从电子卡汇总里取出微信充值，手动增加,再取出车队卡和实体卡
        List<ClassSettlementCard> transactionSummary = new ArrayList<>();
        if(notisStaff){
            transactionSummary=  getTransactionSummary(settDto, params);
        }else{
            transactionSummary=  getStaffTransactionSummary(settDto, params);
        }
        List<CouponSummary> couponSummary = this.queryCouponSum(params);
        CouponSummary CouponSummaryAll = new CouponSummary();
        CouponSummaryAll.setCouponType("合计");
        CouponSummaryAll.setCouponAmt(BigDecimal.ZERO);
        CouponSummaryAll.setCount(0);
        for(CouponSummary coupon :couponSummary){
            CouponSummaryAll.setCount(CouponSummaryAll.getCount()+coupon.getCount());
            CouponSummaryAll.setCouponAmt(CouponSummaryAll.getCouponAmt().add(coupon.getCouponAmt()));
        }
        couponSummary.add(CouponSummaryAll);
        //        取出退款汇总
        List<ClassSettlement> refundSummary = getRefundSummary(settDto, params);
        Map<String, Object> queryGroupByActivitySum = new HashMap<String, Object>();
        for (Map<String, Object> stringObjectMap : queryGroupByActivity) {
            // 检查键"order_count"是否存在
            if (stringObjectMap.containsKey("order_count")) {
                queryGroupByActivitySum.put("order_count", queryGroupByActivitySum.containsKey("order_count")
                        ? Long.valueOf(queryGroupByActivitySum.get("order_count").toString()) + Long.valueOf(stringObjectMap.get("order_count").toString())
                        : Long.valueOf(stringObjectMap.get("order_count").toString()));
            }

            // 检查键"discount_amt"是否存在
            if (stringObjectMap.containsKey("discount_amt")) {
                queryGroupByActivitySum.put("discount_amt", queryGroupByActivitySum.containsKey("discount_amt")
                        ? Double.valueOf(queryGroupByActivitySum.get("discount_amt").toString()) + Double.valueOf(stringObjectMap.get("discount_amt").toString())
                        : Double.valueOf(stringObjectMap.get("discount_amt").toString()));
            }

            // 检查键"order_liters"是否存在
            if (stringObjectMap.containsKey("order_liters")) {
                queryGroupByActivitySum.put("order_liters", queryGroupByActivitySum.containsKey("order_liters")
                        ? Double.valueOf(queryGroupByActivitySum.get("order_liters").toString()) + Double.valueOf(stringObjectMap.get("order_liters").toString())
                        : Double.valueOf(stringObjectMap.get("order_liters").toString()));
            }

            // 检查键"receivable_amt"是否存在
            if (stringObjectMap.containsKey("receivable_amt")) {
                queryGroupByActivitySum.put("receivable_amt", queryGroupByActivitySum.containsKey("receivable_amt")
                        ? Double.valueOf(queryGroupByActivitySum.get("receivable_amt").toString()) + Double.valueOf(stringObjectMap.get("receivable_amt").toString())
                        : Double.valueOf(stringObjectMap.get("receivable_amt").toString()));
            }

            if (stringObjectMap.containsKey("amt")) {
                queryGroupByActivitySum.put("amt", queryGroupByActivitySum.containsKey("amt")
                        ? Double.valueOf(queryGroupByActivitySum.get("amt").toString()) + Double.valueOf(stringObjectMap.get("amt").toString())
                        : Double.valueOf(stringObjectMap.get("amt").toString()));
            }
        }
//        消费合计应收：receivableAmt (应收金额)
//        短款：moreAmt (短款金额)
//        消费合计实收：amt (实收金额)
//        优惠券合计金额：discountCouponAmt (优惠券优惠金额)
//        充值实收合计金额：(写死为0)（未提供充值实收合计金额字段）
//        立减合计金额：discountAmt (立减优惠金额)
//        客户退款合计金额：refundAmt (退款金额)
//        消耗积分：score (消耗积分)
//        备注：note (备注)
//        提现总金额：totalWithdrawalAmount (提现总金额)
        resultMap.put("classSettlementOilmachines", classSettlementOilmachines);
        resultMap.put("querySummaryByClassNo", querySummaryByClassNo);
        resultMap.put("cardSum", settDto);

        resultMap.put("queryGroupByOil", queryGroupByOil);
        resultMap.put("queryGroupByPayType", queryGroupByPayType);
        resultMap.put("queryGroupDiscountByOil", queryGroupDiscountByOil);
//        加油员充值汇总
        resultMap.put("changeClassSettlement4User", changeClassSettlement4User);
        List<ClassSettlementCard> queryGroupByRecharge = queryGroupByRecharge(queryGroupByPayType, settDto);
        resultMap.put("queryGroupByRecharge", queryGroupByRecharge);
        resultMap.put("classSettlementCards", classSettlementCards);
        resultMap.put("queryGroupByOilGun", queryGroupByOilGun);
        resultMap.put("queryGroupByActivity", queryGroupByActivity);
        resultMap.put("queryGroupByActivitySum", queryGroupByActivitySum);
        resultMap.put("listDetail4User", listDetail4User);
        resultMap.put("transactionSummary", transactionSummary);

        resultMap.put("couponSummary", couponSummary);

        resultMap.put("refundSummary", refundSummary);
        if (querySummaryByClassNo != null && !querySummaryByClassNo.isEmpty()) {
            // querySummaryByClassNo 不为空时的处理逻辑
            BigDecimal sumPrice = querySummaryByClassNo != null && !querySummaryByClassNo.isEmpty() ? querySummaryByClassNo.get(0).getSumPrice() : BigDecimal.ZERO;
            resultMap.put("accountAmt", MathUtil.add(sumPrice, settDto.getTotalAmt()));
            if (querySummaryByClassNo.get(0).getServiceCharge() != null) {
                BigDecimal serviceCharge = querySummaryByClassNo != null && !querySummaryByClassNo.isEmpty() && querySummaryByClassNo.get(0).getServiceCharge() != null ? querySummaryByClassNo.get(0).getServiceCharge() : BigDecimal.ZERO;
                resultMap.put("serviceCharge", MathUtil.add(serviceCharge, settDto.getTotalServiceCharge()));
            } else {
                resultMap.put("serviceCharge", BigDecimal.ZERO);
            }
        } else {
            resultMap.put("accountAmt", BigDecimal.ZERO);
            resultMap.put("serviceCharge", BigDecimal.ZERO);
        }
        return resultMap;
    }



    @Override
    public ReportSettlement selectClassReportmentSummary(ClassSettlement classSettlement) {
        ReportSettlement reportSettlement = classSettlementMapper.selectClassReportmentSummary(classSettlement);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (reportSettlement.getClassStartTime() != null) {

            Date startTime = reportSettlement.getClassStartTime();
            String formattedStartTime = sdf.format(startTime);
            classSettlement.setBeginTime(formattedStartTime);
        } else {
            classSettlement.setBeginTime(null);
        }

        if (reportSettlement.getClassEndTime() != null) {
            Date endTime = reportSettlement.getClassEndTime();
            String formattedEndTime = sdf.format(endTime);
            classSettlement.setEndTime(formattedEndTime);
            reportSettlement.setTimeOnTrade(formattedEndTime);
        } else {
            classSettlement.setEndTime(null);
        }
      BigDecimal a =   classSettlementMapper.totalWordCount(classSettlement);
      if(a != null){
          reportSettlement.setTotalWordCount(a);
      }
        return reportSettlement;
    }

    @Override
    public List<ClassSettlementOilmachine> selectAllClassSettlementOilmachines(String classNo) {

        return classSettlementMapper.selectAllClassSettlementOilmachines(classNo);
    }


    @Override
    public BigDecimal totalWordCount(ClassSettlement classSettlement) {

        return classSettlementMapper.totalWordCount(classSettlement);
    }

    @Override
    public List<ClassSettlement> selectClassSettlementSummary4POSList(Map<String, Object> params) {
        return classSettlementMapper.selectClassSettlementSummary4POSList(params);
    }

    @Override
    public AjaxResult getInfoForOrder(String createStartTime,
                                      String createEndTime,
                                      Long stationId,
                                      CardClassSettDto settDto,
                                    int openStaffClassInStation
                                      ) {

        if (stationId == null || stationId.equals("")) {
            return AjaxResult.error("没有油站");
        }
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("createStartTime", createStartTime);
        queryMap.put("createEndTime", createEndTime);
        queryMap.put("stationId", stationId);
        if(openStaffClassInStation == 1){
            queryMap.put("openStaffClassInStation", 1);
        }
        List<ClassSettlement> querySummaryByClassNo = this.querySummaryByClassNoForOrder(queryMap);
// 根据班结编号查询班结摘要信息
        List<Map<String, Object>> queryGroupByOil = this.queryGroupByOilForOrder(queryMap);
        Iterator<Map<String, Object>> iterator = queryGroupByOil.iterator();
        while (iterator.hasNext()) {
            Map<String, Object> map = iterator.next();
            if (!map.containsKey("oil_name")) {
                iterator.remove();
            }
        }
        Map<String, Object> allOil = new HashMap<String, Object>();
        BigDecimal orderLiterSum = BigDecimal.ZERO;
        BigDecimal receivableAmtSum = BigDecimal.ZERO;
        BigDecimal amtSum = BigDecimal.ZERO;
        BigDecimal discountAmtSum = BigDecimal.ZERO;
        BigDecimal bargainDiscountAmt = BigDecimal.ZERO;
        BigDecimal discountCouponAmtSum = BigDecimal.ZERO;
        BigDecimal orderCountSum = BigDecimal.ZERO;
        for (Map<String, Object> itemOil : queryGroupByOil) {
            Object orderLiterObj = itemOil.get("order_liters");
            double orderLiterDouble = (orderLiterObj != null) ? (double) orderLiterObj : 0.0;
            BigDecimal orderLiter = BigDecimal.valueOf(orderLiterDouble);
            if (orderLiter != null) {
                orderLiterSum = orderLiterSum.add(orderLiter);
            }

            Object receivableAmtObj = itemOil.get("receivable_amt");
            double receivableAmtDouble = (receivableAmtObj != null) ? (double) receivableAmtObj : 0.0;
            BigDecimal receivableAmt = BigDecimal.valueOf(receivableAmtDouble);
            if (receivableAmt != null) {
                receivableAmtSum = receivableAmtSum.add(receivableAmt);
            }

            Object amtObj = itemOil.get("amt");
            double amtDouble = (amtObj != null) ? (double) amtObj : 0.0;
            BigDecimal amt = BigDecimal.valueOf(amtDouble);
            if (amt != null) {
                amtSum = amtSum.add(amt);
            }

            Object discountAmtObj = itemOil.get("discount_amt");
            double discountAmtDouble = (discountAmtObj != null) ? (double) discountAmtObj : 0.0;
            BigDecimal discountAmt = BigDecimal.valueOf(discountAmtDouble);
            if (discountAmt != null) {
                discountAmtSum = discountAmtSum.add(discountAmt);
            }
            Object bargain_discount_amtObj = itemOil.get("bargain_discount_amt");
            BigDecimal bargain_discount_amt = (bargain_discount_amtObj != null) ? (BigDecimal) bargain_discount_amtObj : BigDecimal.ZERO;
            if (bargain_discount_amt != null) {
                bargainDiscountAmt = bargainDiscountAmt.add(bargain_discount_amt);
            }
            Object discountCouponAmtObj = itemOil.get("discount_coupon_amt");
            double discountCouponAmtDouble = (discountCouponAmtObj != null) ? (double) discountCouponAmtObj : 0.0;
            BigDecimal discountCouponAmt = BigDecimal.valueOf(discountCouponAmtDouble);
            if (discountCouponAmt != null) {
                discountCouponAmtSum = discountCouponAmtSum.add(discountCouponAmt);
            }
            Object orderCountObj = itemOil.get("order_count");
            BigDecimal orderCount = (orderCountObj != null) ? (BigDecimal) orderCountObj : BigDecimal.ZERO;
            BigDecimal orderCountBigDecimal = orderCount;
            if (orderCountBigDecimal != null) {
                orderCountSum = orderCountSum.add(orderCountBigDecimal);
            }
        }
        allOil.put("oil_name", "合计");
        allOil.put("order_liters", orderLiterSum);
        allOil.put("receivable_amt", receivableAmtSum);
        allOil.put("amt", amtSum);
        allOil.put("discount_amt", discountAmtSum);
        allOil.put("bargain_discount_amt", bargainDiscountAmt);
        allOil.put("discount_coupon_amt", discountCouponAmtSum);
        allOil.put("order_count", orderCountSum);
        queryGroupByOil.add(allOil);

// 根据油品分组查询统计信息
        List<Map<String, Object>> queryGroupByPayType = this.queryGroupByPayTypeForOrder(queryMap);
        Iterator<Map<String, Object>> iterator3 = queryGroupByPayType.iterator();
        while (iterator3.hasNext()) {
            Map<String, Object> map = iterator3.next();
            if (!map.containsKey("pay_type")) {
                iterator3.remove();
            }
        }
        Map<String, Object> allPayType = new HashMap<>();
        BigDecimal amtSum2 = BigDecimal.ZERO;
        BigDecimal refundAmtSum2 = BigDecimal.ZERO;
        BigDecimal discountAmtSum2 = BigDecimal.ZERO;
        BigDecimal bargainDiscountAmt2 = BigDecimal.ZERO;
        BigDecimal discountCouponAmtSum2 = BigDecimal.ZERO;
        BigDecimal discountCount2 = BigDecimal.ZERO;

        for (Map<String, Object> itemPayType : queryGroupByPayType) {
            Object amtObj = itemPayType.get("amt");
            double amtDouble = (amtObj != null) ? (double) amtObj : 0.0;
            BigDecimal amt = BigDecimal.valueOf(amtDouble);
            if (amt != null) {
                amtSum2 = amtSum2.add(amt);
            }

            Object refundAmtObj = itemPayType.get("refund_amt");
            double refundAmtDouble = (refundAmtObj != null) ? (double) refundAmtObj : 0.0;
            BigDecimal refundAmt = BigDecimal.valueOf(refundAmtDouble);
            if (refundAmt != null) {
                refundAmtSum2 = refundAmtSum2.add(refundAmt);
            }

            Object discountAmtObj = itemPayType.get("discount_amt");
            double discountAmtDouble = (discountAmtObj != null) ? (double) discountAmtObj : 0.0;
            BigDecimal discountAmt = BigDecimal.valueOf(discountAmtDouble);
            if (discountAmt != null) {
                discountAmtSum2 = discountAmtSum2.add(discountAmt);
            }
            Object bargain_discount_amtObj = itemPayType.get("bargain_discount_amt");
            BigDecimal bargain_discount_amt = (bargain_discount_amtObj != null) ? (BigDecimal) bargain_discount_amtObj : BigDecimal.ZERO;
            if (bargain_discount_amt != null) {
                bargainDiscountAmt2 = bargainDiscountAmt2.add(bargain_discount_amt);
            }
            Object discountCouponAmtObj = itemPayType.get("discount_coupon_amt");
            double discountCouponAmtDouble = (discountCouponAmtObj != null) ? (double) discountCouponAmtObj : 0.0;
            BigDecimal discountCouponAmt = BigDecimal.valueOf(discountCouponAmtDouble);
            if (discountCouponAmt != null) {
                discountCouponAmtSum2 = discountCouponAmtSum2.add(discountCouponAmt);
            }


            Object discountOrderCountObj = itemPayType.get("order_count");
            BigDecimal discountOrderCountint = (discountOrderCountObj != null) ? (BigDecimal) discountOrderCountObj : BigDecimal.ZERO;
            if (discountOrderCountint != null) {
                discountCount2 = discountCount2.add(discountOrderCountint);
            }
        }
        for(Map<String, Object> item :queryGroupByPayType){
            Map<String, Object> OrderPayqueryMap = new HashMap<String, Object>();
            OrderPayqueryMap.put("payType", item.get("pay_type"));
            OrderPayqueryMap.put("stationId", item.get("station_id"));
            String orderPayType = classSettlementMapper.queryOrderPayType(OrderPayqueryMap);
            if(orderPayType!=null){
                item.put("pay_type",orderPayType);
            }
        }
        allPayType.put("pay_type", "合计");
        allPayType.put("amt", amtSum2);
        allPayType.put("refund_amt", refundAmtSum2);
        allPayType.put("discount_amt", discountAmtSum2);
        allPayType.put("bargain_discount_amt", bargainDiscountAmt2);
        allPayType.put("order_count", discountCount2);
        allPayType.put("discount_coupon_amt", discountCouponAmtSum2);
        queryGroupByPayType.add(allPayType);

        List<Map<String, Object>> queryGroupDiscountByOil = classSettlementMapper.employeeQueryGroupDiscountByOilForOrder(queryMap);
        Iterator<Map<String, Object>> iteratorDiscountByOil = queryGroupDiscountByOil.iterator();
        while (iteratorDiscountByOil.hasNext()) {
            Map<String, Object> map = iteratorDiscountByOil.next();
            if (!map.containsKey("oil_name")) {
                iteratorDiscountByOil.remove();
            }
        }
        Map<String, Object> allDiscountByOil = new HashMap<>();
        BigDecimal allDiscountByOilRechargeDiscountAmtSum2 = BigDecimal.ZERO;
        BigDecimal allDiscountByOilConsumeDiscountAmtSum2 = BigDecimal.ZERO;
        BigDecimal allDiscountByOilDiscountCouponAmtSum2 = BigDecimal.ZERO;
        BigDecimal allDiscount = BigDecimal.ZERO;

        for (Map<String, Object> itemPayType : queryGroupDiscountByOil) {

            Object recharge_discount_amtObj = itemPayType.get("recharge_discount_amt");
            BigDecimal recharge_discount_amtBigDecimal = (recharge_discount_amtObj != null) ? (BigDecimal) recharge_discount_amtObj :BigDecimal.ZERO;

            if (recharge_discount_amtBigDecimal != null) {
                allDiscountByOilRechargeDiscountAmtSum2 = allDiscountByOilRechargeDiscountAmtSum2.add(recharge_discount_amtBigDecimal);
            }

            Object consume_discount_amtObj = itemPayType.get("consume_discount_amt");
            BigDecimal consume_discount_amtBigDecimal = (consume_discount_amtObj != null) ? (BigDecimal) consume_discount_amtObj :BigDecimal.ZERO;

            if (consume_discount_amtBigDecimal != null) {
                allDiscountByOilConsumeDiscountAmtSum2 = allDiscountByOilConsumeDiscountAmtSum2.add(consume_discount_amtBigDecimal);
            }

            Object all_discountObj = itemPayType.get("all_discount");
            BigDecimal all_discountBigDecimal = (all_discountObj != null) ? (BigDecimal) all_discountObj :BigDecimal.ZERO;

            if (all_discountBigDecimal != null) {
                allDiscount = allDiscount.add(all_discountBigDecimal);
            }


            Object discountCouponAmtObj = itemPayType.get("discount_coupon_amt");
            double discountCouponAmtDouble = (discountCouponAmtObj != null) ? (double) discountCouponAmtObj : 0.0;
            BigDecimal discountCouponAmt = BigDecimal.valueOf(discountCouponAmtDouble);
            if (discountCouponAmt != null) {
                allDiscountByOilDiscountCouponAmtSum2 = allDiscountByOilDiscountCouponAmtSum2.add(discountCouponAmt);
            }
        }

        allDiscountByOil.put("oil_name", "合计");
        allDiscountByOil.put("all_discount", allDiscount);
        allDiscountByOil.put("recharge_discount_amt", allDiscountByOilRechargeDiscountAmtSum2);
        allDiscountByOil.put("consume_discount_amt", allDiscountByOilConsumeDiscountAmtSum2);
        allDiscountByOil.put("discount_coupon_amt", allDiscountByOilDiscountCouponAmtSum2);

        queryGroupDiscountByOil.add(allDiscountByOil);

// 根据支付方式分组查询统计信息
        List<Map<String, Object>> queryGroupByOilGun = this.queryGroupByOilGunForOrder(queryMap);
        Iterator<Map<String, Object>> iterator2 = queryGroupByOilGun.iterator();
        while (iterator2.hasNext()) {
            Map<String, Object> map = iterator2.next();
            if (!map.containsKey("oil_gun")) {
                iterator2.remove();
            }
        }
        List<OilEngineDto> newQueryGroupByOilGun = classSettlementMapper.classSettlementOilEngineSum(queryMap);
        List<String> oilGunListA = new ArrayList<>();
        List<String> oilGunListB = new ArrayList<>();
        if (queryGroupByOilGun.size() > 0 && newQueryGroupByOilGun.size() > 0) {
            for (Map<String, Object> map : queryGroupByOilGun) {
                String oilGun = map.get("oil_gun").toString();
                oilGunListA.add(oilGun); // 存储a列表的油枪数据
                oilGunListB.clear();
                for (OilEngineDto oilEngineDto : newQueryGroupByOilGun) {
                    oilGunListB.add(oilEngineDto.getOilGun());
                    if (oilEngineDto != null && oilEngineDto.getOilGun() != null && oilEngineDto.getOilGun().equals(oilGun)) {
                        map.put("oil_price", oilEngineDto.getOilPrice());
                        map.put("order_liters", oilEngineDto.getOrderLiters());
                        map.put("oil_name", oilEngineDto.getOilName());
                        map.put("startFlowMeter", oilEngineDto.getStartFlowMeter());
                        map.put("endFlowMeter", oilEngineDto.getEndFlowMeter());
                        map.put("totalWordCount", 0);
                        // 获取 startFlowMeter 和 endFlowMeter 的值
                        Double startFlowMeterValue = oilEngineDto.getStartFlowMeter();
                        Double endFlowMeterValue = oilEngineDto.getEndFlowMeter();
                        // 空值检查
                        if (startFlowMeterValue != null && endFlowMeterValue != null) {
                            // 计算差值
                            Double diff = endFlowMeterValue - startFlowMeterValue;

                            // 将差值放入 map 中
                            map.put("totalWordCount", diff);
                        }
                    }
                }
            }
            List<String> difference = new ArrayList<>();
            for (String value : oilGunListB) {
                if (!oilGunListA.contains(value)) {
                    difference.add(value);
                }
            }
            for (OilEngineDto oilEngineDto : newQueryGroupByOilGun) {
                for (String oilGun : difference) {

                    if (oilEngineDto != null && oilEngineDto.getOilGun() != null && oilEngineDto.getOilGun().equals(oilGun)) {

                        Map<String, Object> map = new HashMap<>();
                        map.put("oil_gun", oilEngineDto.getOilGun());
                        map.put("oil_price", oilEngineDto.getOilPrice());
                        map.put("order_liters", oilEngineDto.getOrderLiters());
                        map.put("oil_name", oilEngineDto.getOilName());
                        map.put("startFlowMeter", oilEngineDto.getStartFlowMeter());
                        map.put("endFlowMeter", oilEngineDto.getEndFlowMeter());
                        map.put("totalWordCount", 0);
                        // 获取 startFlowMeter 和 endFlowMeter 的值
                        Double startFlowMeterValue = oilEngineDto.getStartFlowMeter();
                        Double endFlowMeterValue = oilEngineDto.getEndFlowMeter();
                        // 空值检查
                        if (startFlowMeterValue != null && endFlowMeterValue != null) {
                            // 计算差值
                            Double diff = endFlowMeterValue - startFlowMeterValue;

                            // 将差值放入 map 中
                            map.put("totalWordCount", diff);
                        }
                        queryGroupByOilGun.add(map);
                    }
                }
            }

        } else if (queryGroupByOilGun.size() == 0 && newQueryGroupByOilGun.size() > 0) {
            for (OilEngineDto oilEngineDto : newQueryGroupByOilGun) {
                if (oilEngineDto != null && oilEngineDto.getOilGun() != null) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("oil_gun", oilEngineDto.getOilGun());
                    map.put("oil_price", oilEngineDto.getOilPrice());
                    map.put("order_liters", oilEngineDto.getOrderLiters());
                    map.put("oil_name", oilEngineDto.getOilName());
                    map.put("startFlowMeter", oilEngineDto.getStartFlowMeter());
                    map.put("endFlowMeter", oilEngineDto.getEndFlowMeter());
                    map.put("totalWordCount", 0);
                    // 获取 startFlowMeter 和 endFlowMeter 的值
                    Double startFlowMeterValue = oilEngineDto.getStartFlowMeter();
                    Double endFlowMeterValue = oilEngineDto.getEndFlowMeter();
                    // 空值检查
                    if (startFlowMeterValue != null && endFlowMeterValue != null) {
                        // 计算差值
                        Double diff = endFlowMeterValue - startFlowMeterValue;

                        // 将差值放入 map 中
                        map.put("totalWordCount", diff);
                    }
                    queryGroupByOilGun.add(map);

                }
            }
        }

// 根据油枪号分组查询统计信息
        List<Map<String, Object>> queryGroupByActivity = this.queryGroupByActivityForOrder(queryMap);
// 根据活动分组查询统计信息
        List<Map<String, Object>> listDetail4User = this.selectClassSettlement4User4POSListForOrder(queryMap);
        Iterator<Map<String, Object>> iterator4 = listDetail4User.iterator();
        while (iterator4.hasNext()) {
            Map<String, Object> map = iterator4.next();
            if (!map.containsKey("oil_name")) {
                iterator4.remove();
            }
        }
        Map<String, Object> allDetail4User = new HashMap<String, Object>();
        BigDecimal orderLiterSumForUser = BigDecimal.ZERO;
        BigDecimal receivableAmtSumForUser = BigDecimal.ZERO;
        BigDecimal amtSumForUser = BigDecimal.ZERO;
        BigDecimal discountAmtSumForUser = BigDecimal.ZERO;
        BigDecimal discountCouponAmtSumForUser = BigDecimal.ZERO;
        BigDecimal refundAmtSumForUser = BigDecimal.ZERO;
        BigDecimal orderCountSumForUser = BigDecimal.ZERO;
        BigDecimal moreAmtSumForUser = BigDecimal.ZERO;

        for (Map<String, Object> itemOil : listDetail4User) {
            Object orderLiterObj = itemOil.get("order_liters");
            double orderLiterDouble = (orderLiterObj != null) ? (double) orderLiterObj : 0.0;
            BigDecimal orderLiter = BigDecimal.valueOf(orderLiterDouble);
            if (orderLiter != null) {
                orderLiterSumForUser = orderLiterSumForUser.add(orderLiter);
            }

            Object receivableAmtObj = itemOil.get("receivable_amt");
            double receivableAmtDouble = (receivableAmtObj != null) ? (double) receivableAmtObj : 0.0;
            BigDecimal receivableAmt = BigDecimal.valueOf(receivableAmtDouble);
            if (receivableAmt != null) {
                receivableAmtSumForUser = receivableAmtSumForUser.add(receivableAmt);
            }

            Object amtObj = itemOil.get("amt");
            double amtDouble = (amtObj != null) ? (double) amtObj : 0.0;
            BigDecimal amt = BigDecimal.valueOf(amtDouble);
            if (amt != null) {
                amtSumForUser = amtSumForUser.add(amt);
            }

            Object discountAmtObj = itemOil.get("discount_amt");
            double discountAmtDouble = (discountAmtObj != null) ? (double) discountAmtObj : 0.0;
            BigDecimal discountAmt = BigDecimal.valueOf(discountAmtDouble);
            if (discountAmt != null) {
                discountAmtSumForUser = discountAmtSumForUser.add(discountAmt);
            }

            Object discountCouponAmtObj = itemOil.get("discount_coupon_amt");
            double discountCouponAmtDouble = (discountCouponAmtObj != null) ? (double) discountCouponAmtObj : 0.0;
            BigDecimal discountCouponAmt = BigDecimal.valueOf(discountCouponAmtDouble);
            if (discountCouponAmt != null) {
                discountCouponAmtSumForUser = discountCouponAmtSumForUser.add(discountCouponAmt);
            }

            Object refundAmtObj = itemOil.get("refund_amt");
            double refundAmtDouble = (refundAmtObj != null) ? (double) refundAmtObj : 0.0;
            BigDecimal refundAmt = BigDecimal.valueOf(refundAmtDouble);
            if (refundAmt != null) {
                refundAmtSumForUser = refundAmtSumForUser.add(refundAmt);
            }

            Object orderCountObj = itemOil.get("order_count");
            BigDecimal orderCount = (orderCountObj != null) ? (BigDecimal) orderCountObj : BigDecimal.ZERO;
            BigDecimal orderCountBigDecimal = orderCount;
            if (orderCountBigDecimal != null) {
                orderCountSumForUser = orderCountSumForUser.add(orderCountBigDecimal);
            }

            Object moreAmtObj = itemOil.get("more_amt");
            double moreAmtDouble = (moreAmtObj != null) ? (double) moreAmtObj : 0.0;
            BigDecimal moreAmt = BigDecimal.valueOf(moreAmtDouble);
            if (moreAmt != null) {
                moreAmtSumForUser = moreAmtSumForUser.add(moreAmt);
            }
        }

        allDetail4User.put("oil_personnel_name", "合计");
        allDetail4User.put("order_liters", orderLiterSumForUser);
        allDetail4User.put("receivable_amt", receivableAmtSumForUser);
        allDetail4User.put("amt", amtSumForUser);
        allDetail4User.put("discount_amt", discountAmtSumForUser);
        allDetail4User.put("discount_coupon_amt", discountCouponAmtSumForUser);
        allDetail4User.put("refund_amt", refundAmtSumForUser);
        allDetail4User.put("order_count", orderCountSumForUser);
        allDetail4User.put("more_amt", moreAmtSumForUser);

        listDetail4User.add(allDetail4User);
// 根据查询条件查询用户的班结详细信息列表
        ClassSettlementCard classSettlementCard = new ClassSettlementCard();
        // 确保 createStartTime 不为 null
        if (createStartTime != null) {
            // 保留年月日时分秒部分，截取前19个字符
            String formattedStartTime = createStartTime.substring(0, 19);
            classSettlementCard.setCreateStartTime(formattedStartTime);

        }
// 确保 createEndTime 不为 null
        if (createEndTime != null) {
            // 保留年月日时分秒部分，截取前19个字符
            String formattedEndTime = createEndTime.substring(0, 19);
            classSettlementCard.setCreateEndTime(formattedEndTime);

        }
        classSettlementCard.setStationId(stationId);
        List<changeClassSettlement4User> changeClassSettlement4User = classSettlementMapper.selectChange4User4POSListForOrder(queryMap);
        changeClassSettlement4User allChangeForUser = new changeClassSettlement4User();
        allChangeForUser.setOilPersonnelName("合计");
        allChangeForUser.setOrderCount(0);
        allChangeForUser.setAmt(BigDecimal.ZERO);
        for(changeClassSettlement4User change4User:changeClassSettlement4User){
            allChangeForUser.setOrderCount(allChangeForUser.getOrderCount()+change4User.getOrderCount());
            allChangeForUser.setAmt(allChangeForUser.getAmt().add(change4User.getAmt()));
        }
        changeClassSettlement4User.add(allChangeForUser);

        List<ClassSettlementCard> classSettlementCards = classSettlementCardService.queryGroupByPayTypeForOrder(classSettlementCard);
// 根据支付方式分组查询结算卡信息列表
        List<ClassSettlementCard> classSettlementCardsGroupByCardType = classSettlementCardService.queryGroupByCardTypeForOrder(classSettlementCard);
// 根据卡类型分组查询结算卡信息列表
        List<ClassSettlementCard> classSettlementCardRechargeCardType = classSettlementCardService.queryGroupByRechargeCardTypeForOrder(classSettlementCard);
        // 根据充值卡类型分组查询结算卡信息列表
        ClassSettlementCard cardSum = new ClassSettlementCard();
        cardSum.setAmt(BigDecimal.ZERO);
        cardSum.setOrderCount(0L);
        cardSum.setServiceCharge(BigDecimal.ZERO);
        for (ClassSettlementCard settlementCard : classSettlementCards) {
            cardSum.setAmt(cardSum.getAmt().add(settlementCard.getAmt()== null ? BigDecimal.ZERO : settlementCard.getAmt()));
            cardSum.setOrderCount(cardSum.getOrderCount() + settlementCard.getOrderCount());
            cardSum.setServiceCharge(cardSum.getServiceCharge().add(settlementCard.getServiceCharge() == null ? BigDecimal.ZERO : settlementCard.getServiceCharge()));
        }

        Map<String, Object> queryGroupByActivitySum = new HashMap<String, Object>();
        for (Map<String, Object> stringObjectMap : queryGroupByActivity) {
            // 检查键"order_count"是否存在
            if (stringObjectMap.containsKey("order_count")) {
                // 如果queryGroupByActivitySum中已经存在"order_count"键，则将其值与stringObjectMap中的"order_count"值相加
                // 否则，将stringObjectMap中的"order_count"值直接放入queryGroupByActivitySum中
                queryGroupByActivitySum.put("order_count", queryGroupByActivitySum.containsKey("order_count")
                        ? Long.valueOf(queryGroupByActivitySum.get("order_count").toString()) + Long.valueOf(stringObjectMap.get("order_count").toString())
                        : Long.valueOf(stringObjectMap.get("order_count").toString()));
            }

            // 检查键"discount_amt"是否存在
            if (stringObjectMap.containsKey("discount_amt")) {
                // 如果queryGroupByActivitySum中已经存在"discount_amt"键，则将其值与stringObjectMap中的"discount_amt"值相加
                // 否则，将stringObjectMap中的"discount_amt"值直接放入queryGroupByActivitySum中
                queryGroupByActivitySum.put("discount_amt", queryGroupByActivitySum.containsKey("discount_amt")
                        ? Double.valueOf(queryGroupByActivitySum.get("discount_amt").toString()) + Double.valueOf(stringObjectMap.get("discount_amt").toString())
                        : Double.valueOf(stringObjectMap.get("discount_amt").toString()));
            }

            // 检查键"order_liters"是否存在
            if (stringObjectMap.containsKey("order_liters")) {
                // 如果queryGroupByActivitySum中已经存在"order_liters"键，则将其值与stringObjectMap中的"order_liters"值相加
                // 否则，将stringObjectMap中的"order_liters"值直接放入queryGroupByActivitySum中
                queryGroupByActivitySum.put("order_liters", queryGroupByActivitySum.containsKey("order_liters")
                        ? Double.valueOf(queryGroupByActivitySum.get("order_liters").toString()) + Double.valueOf(stringObjectMap.get("order_liters").toString())
                        : Double.valueOf(stringObjectMap.get("order_liters").toString()));
            }

            // 检查键"receivable_amt"是否存在
            if (stringObjectMap.containsKey("receivable_amt")) {
                // 如果queryGroupByActivitySum中已经存在"receivable_amt"键，则将其值与stringObjectMap中的"receivable_amt"值相加
                // 否则，将stringObjectMap中的"receivable_amt"值直接放入queryGroupByActivitySum中
                queryGroupByActivitySum.put("receivable_amt", queryGroupByActivitySum.containsKey("receivable_amt")
                        ? Double.valueOf(queryGroupByActivitySum.get("receivable_amt").toString()) + Double.valueOf(stringObjectMap.get("receivable_amt").toString())
                        : Double.valueOf(stringObjectMap.get("receivable_amt").toString()));
            }

            if (stringObjectMap.containsKey("amt")) {
                queryGroupByActivitySum.put("amt", queryGroupByActivitySum.containsKey("amt")
                        ? Double.valueOf(queryGroupByActivitySum.get("amt").toString()) + Double.valueOf(stringObjectMap.get("amt").toString())
                        : Double.valueOf(stringObjectMap.get("amt").toString()));
            }
        }
//        消费合计应收：receivableAmt (应收金额)
//        短款：moreAmt (短款金额)
//        消费合计实收：amt (实收金额)
//        优惠券合计金额：discountCouponAmt (优惠券优惠金额)
//        充值实收合计金额：(写死为0)（未提供充值实收合计金额字段）
//        立减合计金额：discountAmt (立减优惠金额)
//        客户退款合计金额：refundAmt (退款金额)
//        消耗积分：score (消耗积分)
//        备注：note (备注)
//        提现总金额：totalWithdrawalAmount (提现总金额)
        resultMap.put("querySummaryByClassNo", querySummaryByClassNo);
        resultMap.put("cardSum", cardSum);
        resultMap.put("queryGroupByOil", queryGroupByOil);
        resultMap.put("queryGroupByPayType", queryGroupByPayType);
        resultMap.put("queryGroupDiscountByOil", queryGroupDiscountByOil);
        List<ClassSettlementCard> queryGroupByRecharge = new ArrayList<ClassSettlementCard>();
        ClassSettlementCard dkqClassSettlementCard = new ClassSettlementCard();
        dkqClassSettlementCard.setPayType("dkq");
        dkqClassSettlementCard.setGiveAmt(BigDecimal.ZERO);
        dkqClassSettlementCard.setAmt(BigDecimal.ZERO);
        dkqClassSettlementCard.setOrderCount(0L);
        Map<String, Object> dzkCus = null;
        if (queryGroupByPayType != null && !queryGroupByPayType.isEmpty()) {
            for (Map<String, Object> obj : queryGroupByPayType) {
                if (obj.containsKey("pay_type") && obj.get("pay_type").equals("dkq")) {
                    dkqClassSettlementCard.setGiveAmt(BigDecimal.ZERO);
                    double amtDouble = (obj.get("amt") != null) ? (double) obj.get("amt") : 0.0;
                    BigDecimal amt = BigDecimal.valueOf(amtDouble);
                    dkqClassSettlementCard.setAmt(amt); // 强制类型转换
                    Long order_count = (obj.get("order_count") != null) ? ((BigDecimal) obj.get("order_count")).longValue() : 0L;
                    dkqClassSettlementCard.setOrderCount(order_count); // 强制类型转换
                }
                if (obj.containsKey("pay_type") && obj.get("pay_type").equals("dzk")) {
                    dzkCus = obj;
                }
            }
        }
        queryGroupByRecharge.add(dkqClassSettlementCard);
        List<ClassSettlementCard> classSettlementDZK = settDto.getSettlementCardList();
        for(ClassSettlementCard classSettlementDZKItem:classSettlementDZK){
            if(classSettlementDZKItem.getPayType().equals("msub")){
                ClassSettlementCard newClassSettlementCard = new ClassSettlementCard();
                newClassSettlementCard.setPayType("msub");
                newClassSettlementCard.setGiveAmt(BigDecimal.ZERO);
                newClassSettlementCard.setAmt(MathUtil.add(classSettlementDZKItem.getAmt(),classSettlementDZKItem.getGiveAmt()));
                newClassSettlementCard.setOrderCount(classSettlementDZKItem.getOrderCount());
                queryGroupByRecharge.add(newClassSettlementCard);
            }
//            else if(classSettlementDZKItem.getPayType().equals("")){
//                ClassSettlementCard newClassSettlementCard = new ClassSettlementCard();
//                newClassSettlementCard.setPayType("");
//                newClassSettlementCard.setGiveAmt(classSettlementDZKItem.getGiveAmt());
//                newClassSettlementCard.setAmt(classSettlementDZKItem.getAmt());
//                newClassSettlementCard.setOrderCount(classSettlementDZKItem.getOrderCount());
//                queryGroupByRecharge.add(newClassSettlementCard);
//            }
        }
        if (dzkCus != null) {
            ClassSettlementCard newClassSettlementCard = new ClassSettlementCard();
            newClassSettlementCard.setPayType("");
            double discountAmtDouble = (dzkCus.get("discount_amt") != null) ? (double) dzkCus.get("discount_amt") : 0d;
            double discountCouponAmtDouble = (dzkCus.get("discount_coupon_amt") != null) ? (double) dzkCus.get("discount_coupon_amt") : 0d;
            newClassSettlementCard.setGiveAmt(BigDecimal.valueOf(discountAmtDouble).add(BigDecimal.valueOf(discountCouponAmtDouble)));
            newClassSettlementCard.setAmt((dzkCus.get("amt") != null) ? BigDecimal.valueOf((double) dzkCus.get("amt")) : BigDecimal.ZERO);
            newClassSettlementCard.setOrderCount((dzkCus.get("order_count") != null) ? ((BigDecimal) dzkCus.get("order_count")).longValue() : 0L);
            queryGroupByRecharge.add(newClassSettlementCard);
        }
//        充值根据加油员分类充值用户
        resultMap.put("changeClassSettlement4User", changeClassSettlement4User);
        resultMap.put("queryGroupByRecharge", queryGroupByRecharge);
        resultMap.put("classSettlementCards", classSettlementCards);
        resultMap.put("classSettlementCardsGroupByCardType", classSettlementCardsGroupByCardType);
        resultMap.put("classSettlementCardRechargeCardType", classSettlementCardRechargeCardType);
        resultMap.put("queryGroupByOilGun", queryGroupByOilGun);
//        resultMap.put("newQueryGroupByOilGun", newQueryGroupByOilGun);
        resultMap.put("queryGroupByActivity", queryGroupByActivity);
        resultMap.put("queryGroupByActivitySum", queryGroupByActivitySum);
        resultMap.put("listDetail4User", listDetail4User);
        if (querySummaryByClassNo != null && !querySummaryByClassNo.isEmpty()) {
            // querySummaryByClassNo 不为空时的处理逻辑
            BigDecimal sumPrice = querySummaryByClassNo != null && !querySummaryByClassNo.isEmpty() ? querySummaryByClassNo.get(0).getSumPrice() : BigDecimal.ZERO;
            BigDecimal cardSumAmt = cardSum != null && cardSum.getAmt() != null ? cardSum.getAmt() : BigDecimal.ZERO;
            if (sumPrice != null) {
                BigDecimal accountAmt = sumPrice.add(cardSumAmt);
                resultMap.put("accountAmt", accountAmt);
            } else {
                resultMap.put("accountAmt", cardSumAmt);
            }
            if (querySummaryByClassNo.get(0).getServiceCharge() != null) {
                BigDecimal serviceCharge = querySummaryByClassNo != null && !querySummaryByClassNo.isEmpty() && querySummaryByClassNo.get(0).getServiceCharge() != null ? querySummaryByClassNo.get(0).getServiceCharge() : BigDecimal.ZERO;
                BigDecimal cardSumServiceCharge = cardSum != null && cardSum.getServiceCharge() != null ? cardSum.getServiceCharge() : BigDecimal.ZERO;
                if (serviceCharge != null) {
                    BigDecimal totalServiceCharge = serviceCharge.add(cardSumServiceCharge);
                    resultMap.put("serviceCharge", totalServiceCharge);
                } else {
                    resultMap.put("serviceCharge", cardSumServiceCharge);
                }
            } else {
                resultMap.put("serviceCharge", BigDecimal.ZERO);
            }
        } else {
            resultMap.put("accountAmt", BigDecimal.ZERO);
            resultMap.put("serviceCharge", BigDecimal.ZERO);
        }
        return AjaxResult.success(resultMap);
    }

    @Override
    public AjaxResult getEmployeeInfoForOrder(String createStartTime,
                                      String createEndTime,
                                      String oilPersonnelId,
                                              Map<String, Object> params) {

        if (StringUtils.isBlank(oilPersonnelId)) {
            return AjaxResult.error("查看班结详情没有加油员");
        }
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("createStartTime", createStartTime);
        queryMap.put("createEndTime", createEndTime);
        queryMap.put("oilPersonnelId", oilPersonnelId);
        // 根据班结编号查询班结摘要信息
        List<changeClassSettlement4User> changeClassSettlement4User = this.selectChange4User4POSListForOrderStaff(params);
          List<ClassSettlement> querySummaryByClassNo = this.employeeQuerySummaryByClassNoForOrder(queryMap);
// 根据油品分组查询统计信息
        List<Map<String, Object>> queryGroupByOil = this.employeeQueryGroupByOilForOrder(queryMap);
        Iterator<Map<String, Object>> iterator = queryGroupByOil.iterator();
        while (iterator.hasNext()) {
            Map<String, Object> map = iterator.next();
            if (!map.containsKey("oil_name")) {
                iterator.remove();
            }
        }
        Map<String, Object> allOil = new HashMap<String, Object>();
        BigDecimal orderLiterSum = BigDecimal.ZERO;
        BigDecimal receivableAmtSum = BigDecimal.ZERO;
        BigDecimal amtSum = BigDecimal.ZERO;
        BigDecimal discountAmtSum = BigDecimal.ZERO;
        BigDecimal bargainDiscountAmt = BigDecimal.ZERO;
        BigDecimal discountCouponAmtSum = BigDecimal.ZERO;
        BigDecimal orderCountSum = BigDecimal.ZERO;
        for (Map<String, Object> itemOil : queryGroupByOil) {
            Object orderLiterObj = itemOil.get("order_liters");
            double orderLiterDouble = (orderLiterObj != null) ? (double) orderLiterObj : 0.0;
            BigDecimal orderLiter = BigDecimal.valueOf(orderLiterDouble);
            if (orderLiter != null) {
                orderLiterSum = orderLiterSum.add(orderLiter);
            }

            Object receivableAmtObj = itemOil.get("receivable_amt");
            double receivableAmtDouble = (receivableAmtObj != null) ? (double) receivableAmtObj : 0.0;
            BigDecimal receivableAmt = BigDecimal.valueOf(receivableAmtDouble);
            if (receivableAmt != null) {
                receivableAmtSum = receivableAmtSum.add(receivableAmt);
            }

            Object amtObj = itemOil.get("amt");
            double amtDouble = (amtObj != null) ? (double) amtObj : 0.0;
            BigDecimal amt = BigDecimal.valueOf(amtDouble);
            if (amt != null) {
                amtSum = amtSum.add(amt);
            }

            Object discountAmtObj = itemOil.get("discount_amt");
            double discountAmtDouble = (discountAmtObj != null) ? (double) discountAmtObj : 0.0;
            BigDecimal discountAmt = BigDecimal.valueOf(discountAmtDouble);
            if (discountAmt != null) {
                discountAmtSum = discountAmtSum.add(discountAmt);
            }
            Object bargain_discount_amtObj = itemOil.get("bargain_discount_amt");
            BigDecimal bargain_discount_amt = (bargain_discount_amtObj != null) ? (BigDecimal) bargain_discount_amtObj : BigDecimal.ZERO;
            if (bargain_discount_amt != null) {
                bargainDiscountAmt = bargainDiscountAmt.add(bargain_discount_amt);
            }
            Object discountCouponAmtObj = itemOil.get("discount_coupon_amt");
            double discountCouponAmtDouble = (discountCouponAmtObj != null) ? (double) discountCouponAmtObj : 0.0;
            BigDecimal discountCouponAmt = BigDecimal.valueOf(discountCouponAmtDouble);
            if (discountCouponAmt != null) {
                discountCouponAmtSum = discountCouponAmtSum.add(discountCouponAmt);
            }
            Object orderCountObj = itemOil.get("order_count");
            BigDecimal orderCount = (orderCountObj != null) ? (BigDecimal) orderCountObj : BigDecimal.ZERO;
            BigDecimal orderCountBigDecimal = orderCount;
            if (orderCountBigDecimal != null) {
                orderCountSum = orderCountSum.add(orderCountBigDecimal);
            }
        }
        allOil.put("oil_name", "合计");
        allOil.put("order_liters", orderLiterSum);
        allOil.put("receivable_amt", receivableAmtSum);
        allOil.put("amt", amtSum);
        allOil.put("discount_amt", discountAmtSum);
        allOil.put("bargain_discount_amt", bargainDiscountAmt);
        allOil.put("discount_coupon_amt", discountCouponAmtSum);
        allOil.put("order_count", orderCountSum);
        queryGroupByOil.add(allOil);

// 根据支付方式分组查询统计信息
        List<Map<String, Object>> queryGroupByPayType = this.employeeQueryGroupByPayTypeForOrder(queryMap);
        Iterator<Map<String, Object>> iterator3 = queryGroupByPayType.iterator();
        while (iterator3.hasNext()) {
            Map<String, Object> map = iterator3.next();
            if (!map.containsKey("pay_type")) {
                iterator3.remove();
            }
        }
        Map<String, Object> allPayType = new HashMap<>();
        BigDecimal amtSum2 = BigDecimal.ZERO;
        BigDecimal refundAmtSum2 = BigDecimal.ZERO;
        BigDecimal discountAmtSum2 = BigDecimal.ZERO;
        BigDecimal bargainDiscountAmt2 = BigDecimal.ZERO;
        BigDecimal discountCouponAmtSum2 = BigDecimal.ZERO;
        BigDecimal discountCount2 = BigDecimal.ZERO;

        for (Map<String, Object> itemPayType : queryGroupByPayType) {
            Object amtObj = itemPayType.get("amt");
            double amtDouble = (amtObj != null) ? (double) amtObj : 0.0;
            BigDecimal amt = BigDecimal.valueOf(amtDouble);
            if (amt != null) {
                amtSum2 = amtSum2.add(amt);
            }

            Object refundAmtObj = itemPayType.get("refund_amt");
            double refundAmtDouble = (refundAmtObj != null) ? (double) refundAmtObj : 0.0;
            BigDecimal refundAmt = BigDecimal.valueOf(refundAmtDouble);
            if (refundAmt != null) {
                refundAmtSum2 = refundAmtSum2.add(refundAmt);
            }

            Object discountAmtObj = itemPayType.get("discount_amt");
            double discountAmtDouble = (discountAmtObj != null) ? (double) discountAmtObj : 0.0;
            BigDecimal discountAmt = BigDecimal.valueOf(discountAmtDouble);
            if (discountAmt != null) {
                discountAmtSum2 = discountAmtSum2.add(discountAmt);
            }
            Object bargain_discount_amtObj = itemPayType.get("bargain_discount_amt");
            BigDecimal bargain_discount_amt = (bargain_discount_amtObj != null) ? (BigDecimal) bargain_discount_amtObj : BigDecimal.ZERO;
            if (bargain_discount_amt != null) {
                bargainDiscountAmt2 = bargainDiscountAmt2.add(bargain_discount_amt);
            }
            Object discountCouponAmtObj = itemPayType.get("discount_coupon_amt");
            double discountCouponAmtDouble = (discountCouponAmtObj != null) ? (double) discountCouponAmtObj : 0.0;
            BigDecimal discountCouponAmt = BigDecimal.valueOf(discountCouponAmtDouble);
            if (discountCouponAmt != null) {
                discountCouponAmtSum2 = discountCouponAmtSum2.add(discountCouponAmt);
            }
            Object discountOrderCountObj = itemPayType.get("order_count");
            BigDecimal discountOrderCountint = (discountOrderCountObj != null) ? (BigDecimal) discountOrderCountObj : BigDecimal.ZERO;
            if (discountOrderCountint != null) {
                discountCount2 = discountCount2.add(discountOrderCountint);
            }
        }

        allPayType.put("pay_type", "合计");
        allPayType.put("amt", amtSum2);
        allPayType.put("refund_amt", refundAmtSum2);
        allPayType.put("discount_amt", discountAmtSum2);
        allPayType.put("bargain_discount_amt", bargainDiscountAmt2);
        allPayType.put("order_count", discountCount2);
        allPayType.put("discount_coupon_amt", discountCouponAmtSum2);

        queryGroupByPayType.add(allPayType);


           List<Map<String, Object>> queryGroupDiscountByOil = classSettlementMapper.employeeQueryGroupDiscountByOilForOrder(queryMap);
        Iterator<Map<String, Object>> iteratorDiscountByOil = queryGroupDiscountByOil.iterator();
        while (iteratorDiscountByOil.hasNext()) {
            Map<String, Object> map = iteratorDiscountByOil.next();
            if (!map.containsKey("oil_name")) {
                iteratorDiscountByOil.remove();
            }
        }
        Map<String, Object> allDiscountByOil = new HashMap<>();
        BigDecimal allDiscountByOilRechargeDiscountAmtSum2 = BigDecimal.ZERO;
        BigDecimal allDiscountByOilConsumeDiscountAmtSum2 = BigDecimal.ZERO;
        BigDecimal allDiscountByOilDiscountCouponAmtSum2 = BigDecimal.ZERO;
        BigDecimal allDiscount = BigDecimal.ZERO;

        for (Map<String, Object> itemPayType : queryGroupDiscountByOil) {

            Object recharge_discount_amtObj = itemPayType.get("recharge_discount_amt");
            BigDecimal recharge_discount_amtBigDecimal = (recharge_discount_amtObj != null) ? (BigDecimal) recharge_discount_amtObj :BigDecimal.ZERO;

            if (recharge_discount_amtBigDecimal != null) {
                allDiscountByOilRechargeDiscountAmtSum2 = allDiscountByOilRechargeDiscountAmtSum2.add(recharge_discount_amtBigDecimal);
            }

            Object consume_discount_amtObj = itemPayType.get("consume_discount_amt");
            BigDecimal consume_discount_amtBigDecimal = (consume_discount_amtObj != null) ? (BigDecimal) consume_discount_amtObj :BigDecimal.ZERO;

            if (consume_discount_amtBigDecimal != null) {
                allDiscountByOilConsumeDiscountAmtSum2 = allDiscountByOilConsumeDiscountAmtSum2.add(consume_discount_amtBigDecimal);
            }
            Object all_discountObj = itemPayType.get("all_discount");
            BigDecimal all_discountBigDecimal = (all_discountObj != null) ? (BigDecimal) all_discountObj :BigDecimal.ZERO;

            if (all_discountBigDecimal != null) {
                allDiscount = allDiscount.add(all_discountBigDecimal);
            }
            Object discountCouponAmtObj = itemPayType.get("discount_coupon_amt");
            double discountCouponAmtDouble = (discountCouponAmtObj != null) ? (double) discountCouponAmtObj : 0.0;
            BigDecimal discountCouponAmt = BigDecimal.valueOf(discountCouponAmtDouble);
            if (discountCouponAmt != null) {
                allDiscountByOilDiscountCouponAmtSum2 = allDiscountByOilDiscountCouponAmtSum2.add(discountCouponAmt);
            }
        }

        allDiscountByOil.put("oil_name", "合计");
        allDiscountByOil.put("all_discount", allDiscount);
        allDiscountByOil.put("recharge_discount_amt", allDiscountByOilRechargeDiscountAmtSum2);
        allDiscountByOil.put("consume_discount_amt", allDiscountByOilConsumeDiscountAmtSum2);
        allDiscountByOil.put("discount_coupon_amt", allDiscountByOilDiscountCouponAmtSum2);

        queryGroupDiscountByOil.add(allDiscountByOil);

// 根据油枪分组查询统计信息
        List<Map<String, Object>> queryGroupByOilGun = this.employeeQueryGroupByOilGunForOrder(queryMap);
        Iterator<Map<String, Object>> iterator2 = queryGroupByOilGun.iterator();
        while (iterator2.hasNext()) {
            Map<String, Object> map = iterator2.next();
            if (!map.containsKey("oil_gun")) {
                iterator2.remove();
            }
        }
        // 油机数据
        List<OilEngineDto> newQueryGroupByOilGun = classSettlementMapper.employeeClassSettlementOilEngineSum(queryMap);
        List<String> oilGunListA = new ArrayList<>();
        List<String> oilGunListB = new ArrayList<>();
        if (queryGroupByOilGun.size() > 0 && newQueryGroupByOilGun.size() > 0) {
            for (Map<String, Object> map : queryGroupByOilGun) {
                String oilGun = map.get("oil_gun").toString();
                oilGunListA.add(oilGun); // 存储a列表的油枪数据
                oilGunListB.clear();
                for (OilEngineDto oilEngineDto : newQueryGroupByOilGun) {
                    oilGunListB.add(oilEngineDto.getOilGun());
                    if (oilEngineDto != null && oilEngineDto.getOilGun() != null && oilEngineDto.getOilGun().equals(oilGun)) {
                        map.put("oil_price", oilEngineDto.getOilPrice());
                        map.put("order_liters", oilEngineDto.getOrderLiters());
                        map.put("oil_name", oilEngineDto.getOilName());
                        map.put("startFlowMeter", oilEngineDto.getStartFlowMeter());
                        map.put("endFlowMeter", oilEngineDto.getEndFlowMeter());
                        map.put("totalWordCount", 0);
                        // 获取 startFlowMeter 和 endFlowMeter 的值
                        Double startFlowMeterValue = oilEngineDto.getStartFlowMeter();
                        Double endFlowMeterValue = oilEngineDto.getEndFlowMeter();
                        // 空值检查
                        if (startFlowMeterValue != null && endFlowMeterValue != null) {
                            // 计算差值
                            Double diff = endFlowMeterValue - startFlowMeterValue;

                            // 将差值放入 map 中
                            map.put("totalWordCount", diff);
                        }
                    }
                }
            }
            List<String> difference = new ArrayList<>();
            for (String value : oilGunListB) {
                if (!oilGunListA.contains(value)) {
                    difference.add(value);
                }
            }
            for (OilEngineDto oilEngineDto : newQueryGroupByOilGun) {
                for (String oilGun : difference) {

                    if (oilEngineDto != null && oilEngineDto.getOilGun() != null && oilEngineDto.getOilGun().equals(oilGun)) {

                        Map<String, Object> map = new HashMap<>();
                        map.put("oil_gun", oilEngineDto.getOilGun());
                        map.put("oil_price", oilEngineDto.getOilPrice());
                        map.put("order_liters", oilEngineDto.getOrderLiters());
                        map.put("oil_name", oilEngineDto.getOilName());
                        map.put("startFlowMeter", oilEngineDto.getStartFlowMeter());
                        map.put("endFlowMeter", oilEngineDto.getEndFlowMeter());
                        map.put("totalWordCount", 0);
                        // 获取 startFlowMeter 和 endFlowMeter 的值
                        Double startFlowMeterValue = oilEngineDto.getStartFlowMeter();
                        Double endFlowMeterValue = oilEngineDto.getEndFlowMeter();
                        // 空值检查
                        if (startFlowMeterValue != null && endFlowMeterValue != null) {
                            // 计算差值
                            Double diff = endFlowMeterValue - startFlowMeterValue;

                            // 将差值放入 map 中
                            map.put("totalWordCount", diff);
                        }
                        queryGroupByOilGun.add(map);
                    }
                }
            }

        } else if (queryGroupByOilGun.size() == 0 && newQueryGroupByOilGun.size() > 0) {
            for (OilEngineDto oilEngineDto : newQueryGroupByOilGun) {
                if (oilEngineDto != null && oilEngineDto.getOilGun() != null) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("oil_gun", oilEngineDto.getOilGun());
                    map.put("oil_price", oilEngineDto.getOilPrice());
                    map.put("order_liters", oilEngineDto.getOrderLiters());
                    map.put("oil_name", oilEngineDto.getOilName());
                    map.put("startFlowMeter", oilEngineDto.getStartFlowMeter());
                    map.put("endFlowMeter", oilEngineDto.getEndFlowMeter());
                    map.put("totalWordCount", 0);
                    // 获取 startFlowMeter 和 endFlowMeter 的值
                    Double startFlowMeterValue = oilEngineDto.getStartFlowMeter();
                    Double endFlowMeterValue = oilEngineDto.getEndFlowMeter();
                    // 空值检查
                    if (startFlowMeterValue != null && endFlowMeterValue != null) {
                        // 计算差值
                        Double diff = endFlowMeterValue - startFlowMeterValue;

                        // 将差值放入 map 中
                        map.put("totalWordCount", diff);
                    }
                    queryGroupByOilGun.add(map);

                }
            }
        }

// 根据活动分组查询统计信息
//        List<Map<String, Object>> queryGroupByActivity = this.queryGroupByActivityForOrder(queryMap);

// 根据员工方式查询统计信息
        List<Map<String, Object>> listDetail4User = this.employeeSelectClassSettlement4User4POSListForOrder(queryMap);
        Iterator<Map<String, Object>> iterator4 = listDetail4User.iterator();
        while (iterator4.hasNext()) {
            Map<String, Object> map = iterator4.next();
            if (!map.containsKey("oil_name")) {
                iterator4.remove();
            }
        }
        Map<String, Object> allDetail4User = new HashMap<String, Object>();
        BigDecimal orderLiterSumForUser = BigDecimal.ZERO;
        BigDecimal receivableAmtSumForUser = BigDecimal.ZERO;
        BigDecimal amtSumForUser = BigDecimal.ZERO;
        BigDecimal discountAmtSumForUser = BigDecimal.ZERO;
        BigDecimal discountCouponAmtSumForUser = BigDecimal.ZERO;
        BigDecimal refundAmtSumForUser = BigDecimal.ZERO;
        BigDecimal orderCountSumForUser = BigDecimal.ZERO;
        BigDecimal moreAmtSumForUser = BigDecimal.ZERO;

        for (Map<String, Object> itemOil : listDetail4User) {
            Object orderLiterObj = itemOil.get("order_liters");
            double orderLiterDouble = (orderLiterObj != null) ? (double) orderLiterObj : 0.0;
            BigDecimal orderLiter = BigDecimal.valueOf(orderLiterDouble);
            if (orderLiter != null) {
                orderLiterSumForUser = orderLiterSumForUser.add(orderLiter);
            }

            Object receivableAmtObj = itemOil.get("receivable_amt");
            double receivableAmtDouble = (receivableAmtObj != null) ? (double) receivableAmtObj : 0.0;
            BigDecimal receivableAmt = BigDecimal.valueOf(receivableAmtDouble);
            if (receivableAmt != null) {
                receivableAmtSumForUser = receivableAmtSumForUser.add(receivableAmt);
            }

            Object amtObj = itemOil.get("amt");
            double amtDouble = (amtObj != null) ? (double) amtObj : 0.0;
            BigDecimal amt = BigDecimal.valueOf(amtDouble);
            if (amt != null) {
                amtSumForUser = amtSumForUser.add(amt);
            }

            Object discountAmtObj = itemOil.get("discount_amt");
            double discountAmtDouble = (discountAmtObj != null) ? (double) discountAmtObj : 0.0;
            BigDecimal discountAmt = BigDecimal.valueOf(discountAmtDouble);
            if (discountAmt != null) {
                discountAmtSumForUser = discountAmtSumForUser.add(discountAmt);
            }

            Object discountCouponAmtObj = itemOil.get("discount_coupon_amt");
            double discountCouponAmtDouble = (discountCouponAmtObj != null) ? (double) discountCouponAmtObj : 0.0;
            BigDecimal discountCouponAmt = BigDecimal.valueOf(discountCouponAmtDouble);
            if (discountCouponAmt != null) {
                discountCouponAmtSumForUser = discountCouponAmtSumForUser.add(discountCouponAmt);
            }

            Object refundAmtObj = itemOil.get("refund_amt");
            double refundAmtDouble = (refundAmtObj != null) ? (double) refundAmtObj : 0.0;
            BigDecimal refundAmt = BigDecimal.valueOf(refundAmtDouble);
            if (refundAmt != null) {
                refundAmtSumForUser = refundAmtSumForUser.add(refundAmt);
            }

            Object orderCountObj = itemOil.get("order_count");
            BigDecimal orderCount = (orderCountObj != null) ? (BigDecimal) orderCountObj : BigDecimal.ZERO;
            BigDecimal orderCountBigDecimal = orderCount;
            if (orderCountBigDecimal != null) {
                orderCountSumForUser = orderCountSumForUser.add(orderCountBigDecimal);
            }

            Object moreAmtObj = itemOil.get("more_amt");
            double moreAmtDouble = (moreAmtObj != null) ? (double) moreAmtObj : 0.0;
            BigDecimal moreAmt = BigDecimal.valueOf(moreAmtDouble);
            if (moreAmt != null) {
                moreAmtSumForUser = moreAmtSumForUser.add(moreAmt);
            }
        }

        allDetail4User.put("oil_personnel_name", "合计");
        allDetail4User.put("order_liters", orderLiterSumForUser);
        allDetail4User.put("receivable_amt", receivableAmtSumForUser);
        allDetail4User.put("amt", amtSumForUser);
        allDetail4User.put("discount_amt", discountAmtSumForUser);
        allDetail4User.put("discount_coupon_amt", discountCouponAmtSumForUser);
        allDetail4User.put("refund_amt", refundAmtSumForUser);
        allDetail4User.put("order_count", orderCountSumForUser);
        allDetail4User.put("more_amt", moreAmtSumForUser);

        listDetail4User.add(allDetail4User);
// 根据查询条件查询用户的班结详细信息列表
//        ClassSettlementCard classSettlementCard = new ClassSettlementCard();
//        // 确保 createStartTime 不为 null
//        if (createStartTime != null) {
//            // 保留年月日时分秒部分，截取前19个字符
//            String formattedStartTime = createStartTime.substring(0, 19);
//            classSettlementCard.setCreateStartTime(formattedStartTime);
//
//        }
//// 确保 createEndTime 不为 null
//        if (createEndTime != null) {
//            // 保留年月日时分秒部分，截取前19个字符
//            String formattedEndTime = createEndTime.substring(0, 19);
//            classSettlementCard.setCreateEndTime(formattedEndTime);
//
//        }
//        classSettlementCard.setStationId(stationId);
//        List<ClassSettlementCard> classSettlementCards = classSettlementCardService.queryGroupByPayTypeForOrder(classSettlementCard);
// 根据支付方式分组查询结算卡信息列表
//        List<ClassSettlementCard> classSettlementCardsGroupByCardType = classSettlementCardService.queryGroupByCardTypeForOrder(classSettlementCard);
// 根据卡类型分组查询结算卡信息列表
//        List<ClassSettlementCard> classSettlementCardRechargeCardType = classSettlementCardService.queryGroupByRechargeCardTypeForOrder(classSettlementCard);
        // 根据充值卡类型分组查询结算卡信息列表
//        ClassSettlementCard cardSum = new ClassSettlementCard();
//        cardSum.setAmt(BigDecimal.ZERO);
//        cardSum.setOrderCount(0L);
//        cardSum.setServiceCharge(BigDecimal.ZERO);
//        for (ClassSettlementCard settlementCard : classSettlementCards) {
//            cardSum.setAmt(cardSum.getAmt().add(settlementCard.getAmt()));
//            cardSum.setOrderCount(cardSum.getOrderCount() + settlementCard.getOrderCount());
//            cardSum.setServiceCharge(cardSum.getServiceCharge().add(settlementCard.getServiceCharge() == null ? BigDecimal.ZERO : settlementCard.getServiceCharge()));
//        }

//        Map<String, Object> queryGroupByActivitySum = new HashMap<String, Object>();
//        for (Map<String, Object> stringObjectMap : queryGroupByActivity) {
//            // 检查键"order_count"是否存在
//            if (stringObjectMap.containsKey("order_count")) {
//                // 如果queryGroupByActivitySum中已经存在"order_count"键，则将其值与stringObjectMap中的"order_count"值相加
//                // 否则，将stringObjectMap中的"order_count"值直接放入queryGroupByActivitySum中
//                queryGroupByActivitySum.put("order_count", queryGroupByActivitySum.containsKey("order_count")
//                        ? Long.valueOf(queryGroupByActivitySum.get("order_count").toString()) + Long.valueOf(stringObjectMap.get("order_count").toString())
//                        : Long.valueOf(stringObjectMap.get("order_count").toString()));
//            }
//
//            // 检查键"discount_amt"是否存在
//            if (stringObjectMap.containsKey("discount_amt")) {
//                // 如果queryGroupByActivitySum中已经存在"discount_amt"键，则将其值与stringObjectMap中的"discount_amt"值相加
//                // 否则，将stringObjectMap中的"discount_amt"值直接放入queryGroupByActivitySum中
//                queryGroupByActivitySum.put("discount_amt", queryGroupByActivitySum.containsKey("discount_amt")
//                        ? Double.valueOf(queryGroupByActivitySum.get("discount_amt").toString()) + Double.valueOf(stringObjectMap.get("discount_amt").toString())
//                        : Double.valueOf(stringObjectMap.get("discount_amt").toString()));
//            }
//
//            // 检查键"order_liters"是否存在
//            if (stringObjectMap.containsKey("order_liters")) {
//                // 如果queryGroupByActivitySum中已经存在"order_liters"键，则将其值与stringObjectMap中的"order_liters"值相加
//                // 否则，将stringObjectMap中的"order_liters"值直接放入queryGroupByActivitySum中
//                queryGroupByActivitySum.put("order_liters", queryGroupByActivitySum.containsKey("order_liters")
//                        ? Double.valueOf(queryGroupByActivitySum.get("order_liters").toString()) + Double.valueOf(stringObjectMap.get("order_liters").toString())
//                        : Double.valueOf(stringObjectMap.get("order_liters").toString()));
//            }
//
//            // 检查键"receivable_amt"是否存在
//            if (stringObjectMap.containsKey("receivable_amt")) {
//                // 如果queryGroupByActivitySum中已经存在"receivable_amt"键，则将其值与stringObjectMap中的"receivable_amt"值相加
//                // 否则，将stringObjectMap中的"receivable_amt"值直接放入queryGroupByActivitySum中
//                queryGroupByActivitySum.put("receivable_amt", queryGroupByActivitySum.containsKey("receivable_amt")
//                        ? Double.valueOf(queryGroupByActivitySum.get("receivable_amt").toString()) + Double.valueOf(stringObjectMap.get("receivable_amt").toString())
//                        : Double.valueOf(stringObjectMap.get("receivable_amt").toString()));
//            }
//
//            if (stringObjectMap.containsKey("amt")) {
//                queryGroupByActivitySum.put("amt", queryGroupByActivitySum.containsKey("amt")
//                        ? Double.valueOf(queryGroupByActivitySum.get("amt").toString()) + Double.valueOf(stringObjectMap.get("amt").toString())
//                        : Double.valueOf(stringObjectMap.get("amt").toString()));
//            }
//        }
//        消费合计应收：receivableAmt (应收金额)
//        短款：moreAmt (短款金额)
//        消费合计实收：amt (实收金额)
//        优惠券合计金额：discountCouponAmt (优惠券优惠金额)
//        充值实收合计金额：(写死为0)（未提供充值实收合计金额字段）
//        立减合计金额：discountAmt (立减优惠金额)
//        客户退款合计金额：refundAmt (退款金额)
//        消耗积分：score (消耗积分)
//        备注：note (备注)
//        提现总金额：totalWithdrawalAmount (提现总金额)
        resultMap.put("querySummaryByClassNo", querySummaryByClassNo);
        resultMap.put("cardSum", null);
        resultMap.put("queryGroupByOil", queryGroupByOil);  // 消费-油品汇总
        resultMap.put("queryGroupByPayType", queryGroupByPayType);  // 消费-支付方式汇总
        resultMap.put("queryGroupDiscountByOil", queryGroupDiscountByOil);  // 消费-支付方式汇总
        List<ClassSettlementCard> queryGroupByRecharge = new ArrayList<ClassSettlementCard>();
        // 会员卡-消费汇总  实体卡支付
        ClassSettlementCard dkqClassSettlementCard = new ClassSettlementCard();
        dkqClassSettlementCard.setPayType("dkq");
        dkqClassSettlementCard.setGiveAmt(BigDecimal.ZERO);
        dkqClassSettlementCard.setAmt(BigDecimal.ZERO);
        dkqClassSettlementCard.setOrderCount(0L);
        if (queryGroupByPayType != null && !queryGroupByPayType.isEmpty()) {
            for (Map<String, Object> obj : queryGroupByPayType) {
                if (obj.containsKey("pay_type") && obj.get("pay_type").equals("dkq")) {
                    dkqClassSettlementCard.setGiveAmt(BigDecimal.ZERO);
                    double amtDouble = (obj.get("amt") != null) ? (double) obj.get("amt") : 0.0;
                    BigDecimal amt = BigDecimal.valueOf(amtDouble);
                    dkqClassSettlementCard.setAmt(amt); // 强制类型转换
                    Long order_count = (obj.get("order_count") != null) ? ((BigDecimal) obj.get("order_count")).longValue() : 0L;
                    dkqClassSettlementCard.setOrderCount(order_count); // 强制类型转换
                }
            }
        }
        queryGroupByRecharge.add(dkqClassSettlementCard);

        // 手动扣减
//        List<ClassSettlementCard> classSettlementDZK = settDto.getSettlementCardList();
//        for(ClassSettlementCard classSettlementDZKItem:classSettlementDZK){
//            if(classSettlementDZKItem.getPayType().equals("msub")){
//                ClassSettlementCard newClassSettlementCard = new ClassSettlementCard();
//                newClassSettlementCard.setPayType("msub");
//                // 员工权限看不到手动扣减
//                newClassSettlementCard.setGiveAmt(BigDecimal.ZERO);
//                newClassSettlementCard.setAmt(BigDecimal.ZERO);
//                newClassSettlementCard.setOrderCount(0l);
//                queryGroupByRecharge.add(newClassSettlementCard);
//            }else if(classSettlementDZKItem.getPayType().equals("")){
//                ClassSettlementCard newClassSettlementCard = new ClassSettlementCard();
//                newClassSettlementCard.setPayType("");
//                newClassSettlementCard.setGiveAmt(classSettlementDZKItem.getGiveAmt());
//                newClassSettlementCard.setAmt(classSettlementDZKItem.getAmt());
//                newClassSettlementCard.setOrderCount(classSettlementDZKItem.getOrderCount());
//                queryGroupByRecharge.add(newClassSettlementCard);
//            }
//        }

        // 员工权限 手动扣减没有生产订单，无法和员工关联，统计不了  默认设置为0
        ClassSettlementCard newClassSettlementCard = new ClassSettlementCard();
        newClassSettlementCard.setPayType("msub");
        newClassSettlementCard.setGiveAmt(BigDecimal.ZERO);
        newClassSettlementCard.setAmt(BigDecimal.ZERO);
        newClassSettlementCard.setOrderCount(0l);
        queryGroupByRecharge.add(newClassSettlementCard);

        // 会员卡支付（电子卡消费）
        Map<String, Object> dzkMap = classSettlementMapper.employeeSelectDzkPayOrder(queryMap);
        if(dzkMap != null && !dzkMap.isEmpty()){
            ClassSettlementCard newDzkClassSettlementCard = new ClassSettlementCard();
            newClassSettlementCard.setPayType("");
            Object orderCountObj = dzkMap.get("order_count");
            newDzkClassSettlementCard.setOrderCount((orderCountObj != null) ? (Long) orderCountObj : 0l);

            Double amtObj = (Double) dzkMap.get("amt");
            BigDecimal amt = (amtObj != null) ? new BigDecimal(amtObj) : BigDecimal.ZERO;
            newDzkClassSettlementCard.setAmt(amt);

            Double discountAmtObj = (Double) dzkMap.get("discount_amt");
            BigDecimal discountAmt = (discountAmtObj != null) ? new BigDecimal(discountAmtObj) : BigDecimal.ZERO;

            Double discountCouponAmtObj = (Double) dzkMap.get("discount_coupon_amt");
            BigDecimal discountCouponAmt = (discountCouponAmtObj != null) ? new BigDecimal(discountCouponAmtObj) : BigDecimal.ZERO;
            newDzkClassSettlementCard.setGiveAmt(discountAmt.add(discountCouponAmt));

            queryGroupByRecharge.add(newDzkClassSettlementCard);
        }
        resultMap.put("changeClassSettlement4User", changeClassSettlement4User);
        resultMap.put("queryGroupByRecharge", queryGroupByRecharge);  // 会员卡-消费汇总
        resultMap.put("classSettlementCards", null);
        resultMap.put("classSettlementCardsGroupByCardType", null);
        resultMap.put("classSettlementCardRechargeCardType", null);
        resultMap.put("queryGroupByOilGun", queryGroupByOilGun); // 消费-油枪汇总
//        resultMap.put("newQueryGroupByOilGun", newQueryGroupByOilGun);
        resultMap.put("queryGroupByActivity", null);
        resultMap.put("queryGroupByActivitySum", null);
        resultMap.put("listDetail4User", listDetail4User);  // 消费-员工方式汇总
        if (querySummaryByClassNo != null && !querySummaryByClassNo.isEmpty()) {
            // querySummaryByClassNo 不为空时的处理逻辑
            BigDecimal sumPrice = querySummaryByClassNo != null && !querySummaryByClassNo.isEmpty() ? querySummaryByClassNo.get(0).getSumPrice() : BigDecimal.ZERO;
//            BigDecimal cardSumAmt = cardSum != null && cardSum.getAmt() != null ? cardSum.getAmt() : BigDecimal.ZERO;
            if (sumPrice != null) {
//                BigDecimal accountAmt = sumPrice.add(cardSumAmt);
                resultMap.put("accountAmt", BigDecimal.ZERO);
            } else {
                resultMap.put("accountAmt", BigDecimal.ZERO);
            }
            if (querySummaryByClassNo.get(0).getServiceCharge() != null) {
                BigDecimal serviceCharge = querySummaryByClassNo != null && !querySummaryByClassNo.isEmpty() && querySummaryByClassNo.get(0).getServiceCharge() != null ? querySummaryByClassNo.get(0).getServiceCharge() : BigDecimal.ZERO;
//                BigDecimal cardSumServiceCharge = cardSum != null && cardSum.getServiceCharge() != null ? cardSum.getServiceCharge() : BigDecimal.ZERO;
                if (serviceCharge != null) {
//                    BigDecimal totalServiceCharge = serviceCharge.add(cardSumServiceCharge);
                    resultMap.put("serviceCharge", BigDecimal.ZERO);
                } else {
                    resultMap.put("serviceCharge", BigDecimal.ZERO);
                }
            } else {
                resultMap.put("serviceCharge", BigDecimal.ZERO);
            }
        } else {
            resultMap.put("accountAmt", BigDecimal.ZERO);
            resultMap.put("serviceCharge", BigDecimal.ZERO);
        }
        return AjaxResult.success(resultMap);
    }

    /**
     * 新增班结
     *
     * @param classSettlement 班结
     * @return 结果
     */
    @Override
    public int insertClassSettlement(ClassSettlement classSettlement) {
        classSettlement.setCreateTime(DateUtils.getNowDate());
        return classSettlementMapper.insertClassSettlement(classSettlement);
    }

    @Override
    public int insertStaffClassSettlement(ClassSettlement classSettlement) {
        classSettlement.setCreateTime(DateUtils.getNowDate());
        return classSettlementMapper.insertStaffClassSettlement(classSettlement);
    }

    /**
     * 新增日报表
     *
     * @param classSettlement 班结
     * @return 结果
     */
    @Override
    public int insertClassReport(ClassSettlement classSettlement) {
        classSettlement.setCreateTime(DateUtils.getNowDate());
        return classSettlementMapper.insertClassReport(classSettlement);
    }



    @Override
    public void insertBusinessDailyReportAsync(Long stationId,int isClass,String no,Date classEndTime,Date classStartTime){
        this.insertBusinessDailyReport(stationId,isClass,no,classEndTime,classStartTime);
    }

    /**
     * 新增日报表
     *
     * @param stationId 班结
     * @return 结果
     */
    @Override
    public void insertBusinessDailyReport(Long stationId,int isClass,String no,Date classEndTime,Date classStartTime) {

        SysUser user = new SysUser();
        user.setUserId(1L);
        user.setUserName("日报表定时任务");
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat datefmt = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("serviceType", "BGS");//班结的时候多维度分组查询
// 创建一个Calendar对象并将其时间设置为当前时间
        Calendar calendar = Calendar.getInstance();
// 通过Calendar对象的add方法将日期减去一天，以获得昨天的日期
        calendar.add(Calendar.DAY_OF_MONTH, -1);
// 将Calendar对象的时间设置为0点
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.set(Calendar.HOUR_OF_DAY, 0);
        calendar2.set(Calendar.MINUTE, 0);
        calendar2.set(Calendar.SECOND, 0);
        Date today = calendar2.getTime();
// 获取昨天和今天的日期
        Date yesterday = calendar.getTime();
// 格式化昨天和今天的日期为"yyyy-MM-dd HH:mm:ss"的格式
        String nowDateString = datefmt.format(yesterday);

        // 创建一个Calendar对象并将其时间设置为当前时间
        Calendar calendarOnEight = Calendar.getInstance();
// 通过Calendar对象的add方法将日期减去一天，以获得昨天的日期
        calendarOnEight.add(Calendar.DAY_OF_MONTH, -1);
// 将Calendar对象的时间设置为0点
        calendarOnEight.set(Calendar.HOUR_OF_DAY, 8);
        calendarOnEight.set(Calendar.MINUTE, 0);
        calendarOnEight.set(Calendar.SECOND, 0);
        Calendar calendarOnEight2 = Calendar.getInstance();
        calendarOnEight2.set(Calendar.HOUR_OF_DAY, 8);
        calendarOnEight2.set(Calendar.MINUTE, 0);
        calendarOnEight2.set(Calendar.SECOND, 0);
        Date todayEight = calendarOnEight2.getTime();
// 获取昨天和今天的日期
        Date yesterdayEight = calendarOnEight.getTime();


// 将昨天和今天的时间添加到params集合中
        if(isClass == 1){
            if(classStartTime!=null){
                String yesterdayString = fmt.format(classStartTime);
                params.put("createStartTime", yesterdayString);
            }else
            {
                params.put("createStartTime", "2024-01-01 09:00:00");
            }
        String todayString = fmt.format(classEndTime);
        params.put("createEndTime", todayString);
          params.put("stationId", stationId);
        }else if(isClass == 2){
            String yesterdayString = fmt.format(yesterdayEight);
            String todayString = fmt.format(todayEight);
            params.put("createStartTime", yesterdayString);
            params.put("createEndTime", todayString);
            params.put("stationId", stationId);
        }else{
            String yesterdayString = fmt.format(yesterday);
            String todayString = fmt.format(today);
            params.put("createStartTime", yesterdayString);
            params.put("createEndTime", todayString);
            params.put("stationId", stationId);
        }

        BusinessDailyReport businessDailyReport = new BusinessDailyReport();
//        allamt是无效的数据
        BusinessDailyReport  businessDailyReport2 = classSettlementMapper.selectBusinessDailyReport(params);
        if(businessDailyReport2!=null){
            businessDailyReport =businessDailyReport2;
        }
        Station station=  stationMapper.selectStationById(stationId);

        Enterprise enterprise =  enterpriseMapper.selectEnterpriseById(station.getEnterpriseId());
        businessDailyReport.setStationId(stationId);

        businessDailyReport.setStationName(station.getStationName());
        if(station.getStationId().equals(397)||station.getStationId().equals(369)){
            System.out.println(enterprise+station.toString());
        }
        System.out.println(enterprise+station.toString());
        businessDailyReport.setEnterpriseName(enterprise.getEnterpriseName());
        businessDailyReport.setEnterpriseId(station.getEnterpriseId());
        if(isClass == 1){
            if(classStartTime!=null){
                String yesterdayString = fmt.format(classStartTime);
                businessDailyReport.setClassStartTime(yesterdayString);
            }else{
                String yesterdayString =   "2024-01-01 09:00:00";
                businessDailyReport.setClassStartTime(yesterdayString);
            }
            String classEndTimeString = fmt.format(classEndTime);
            businessDailyReport.setClassEndTime(classEndTimeString);
            businessDailyReport.setClassNo(no);
            String todayString = fmt.format(today);
            businessDailyReport.setDate(todayString);
        }else if(isClass == 2){
            String yesterdayString = fmt.format(yesterdayEight);
            String todayString = fmt.format(todayEight);
            String dateString = datefmt.format(todayEight);
            businessDailyReport.setClassStartTime(yesterdayString);
            businessDailyReport.setClassEndTime(todayString);
            businessDailyReport.setDate(dateString);
        }else{
            businessDailyReport.setDate(nowDateString);
        }
        List<ClassSettlementCard> classSettlementCards = classSettlementCardService.queryUntreatedData(params);
        CardClassSettDto settDto = countCardSett(classSettlementCards); // 电子卡交易汇总数据
        ClassSettlementCard physicalCard = classSettlementMapper.queryPhysicalCard(params);
        ClassSettlementCard fleetCard = classSettlementMapper.queryFleetCard(params);
        int integral = classSettlementMapper.queryIntegral(params);
        int writeOffIntegral = classSettlementMapper.queryWriteOffIntegral(params);
        ClassSettlementCard superPay = this.querySuperPay(params);
        ClassSettlementCard forSaleActivity= classSettlementMapper.forSaleActivityAmt(params);
        ClassSettlementCard refSaleActivity= classSettlementMapper.refSaleActivityAmt(params);
        BigDecimal posTransactionAmt = (settDto.getTotalAmt() != null) ?  settDto.getTotalAmt() : BigDecimal.ZERO;
        BigDecimal supTransactionAmt = (superPay.getAmt() != null) ?  superPay.getAmt() : BigDecimal.ZERO;
        BigDecimal dkqTransactionAmt = (physicalCard.getAmt() != null) ?  physicalCard.getAmt() : BigDecimal.ZERO;
        BigDecimal cdkTransactionAmt = (fleetCard.getAmt() != null) ?  fleetCard.getAmt()  : BigDecimal.ZERO;
        BigDecimal supGiftAmt = (superPay.getGiveAmt() != null) ?  superPay.getGiveAmt() : BigDecimal.ZERO;
        BigDecimal dkqGiftAmt = (physicalCard.getGiveAmt() != null) ?  physicalCard.getGiveAmt() : BigDecimal.ZERO;
        BigDecimal cdkGiftAmt = (fleetCard.getGiveAmt() != null) ?  fleetCard.getGiveAmt()  : BigDecimal.ZERO;
//虽然写pos但是其实是电子卡的微信和退款
        BigDecimal posimp = BigDecimal.ZERO;
        BigDecimal posmsub = BigDecimal.ZERO;
        BigDecimal poswx = BigDecimal.ZERO;
        BigDecimal poswxCommission = BigDecimal.ZERO;
        BigDecimal poswxWithdrawalAmount = BigDecimal.ZERO;
        BigDecimal posref = BigDecimal.ZERO;
        BigDecimal posrefCommission = BigDecimal.ZERO;
        BigDecimal posrefWithdrawalAmount = BigDecimal.ZERO;


        BigDecimal forSaleActivityAmt = forSaleActivity.getAmt()!=null?forSaleActivity.getAmt():BigDecimal.ZERO;
        BigDecimal refSaleActivityAmt = refSaleActivity.getAmt()!=null?refSaleActivity.getAmt():BigDecimal.ZERO;
        BigDecimal forSaleActivityCom = forSaleActivity.getServiceCharge()!=null?forSaleActivity.getServiceCharge():BigDecimal.ZERO;
        BigDecimal forSaleActivityWithdrawalAmount = forSaleActivity.getWithdrawalAmount()!=null?forSaleActivity.getWithdrawalAmount():BigDecimal.ZERO;
        List<ClassSettlementCard>  classSettlementCardL = settDto.getSettlementCardList();
//        MANUAL_ADD("imp","手动增加"),
//     *     MANUAL_SUB("msub","手动扣减"),
//     *     WX_RECHARGE("wx","微信充值"),
//        WX_RECHARGE("pos","pos充值"),
//     *     CONSUME("","电子卡支付"),
//     *     REFUND("ref","充值退款"),*/
        for (ClassSettlementCard classSettlementCardS:classSettlementCardL){
            if ( classSettlementCardS.getPayType().equals("imp")){
                if(classSettlementCardS.getAmt()!=null) {
                    posimp = posimp.add(classSettlementCardS.getAmt());
                }
            }
            if ( classSettlementCardS.getPayType().equals("msub")){
                if(classSettlementCardS.getAmt()!=null) {
                    posmsub = posmsub.add(classSettlementCardS.getAmt());
                }
            }
            if ( classSettlementCardS.getPayType().equals("ref")){
                if(classSettlementCardS.getAmt()!=null){
                    posref = posref.add(classSettlementCardS.getAmt());
                }

            }

        }
        businessDailyReport.setSaleActivityWxamt(forSaleActivityAmt);
        businessDailyReport.setWechatConsumption((businessDailyReport.getWechatConsumption()!=null?businessDailyReport.getWechatConsumption():BigDecimal.ZERO));
       BigDecimal weChatRefund = posref.add(refSaleActivityAmt).add(superPay.getRefundAmount()).add(businessDailyReport.getWechatRefund()!=null?businessDailyReport.getWechatRefund():BigDecimal.ZERO);
        businessDailyReport.setWechatRefund(weChatRefund);
        businessDailyReport.setActualRecharge(settDto.getTotalAmt());
        businessDailyReport.setWechatRecharge(settDto.getTotalAmt().add(supTransactionAmt));
        businessDailyReport.setBackstageRecharge(posimp.subtract(posmsub).add(dkqTransactionAmt).add(cdkTransactionAmt));
        businessDailyReport.setRechargeGift(settDto.getTotalGiveAmt().add(dkqGiftAmt));
        ClassSettlementCard physicalCardRef = classSettlementMapper.queryPhysicalCardRef(params);
        businessDailyReport.setPhysicalCardRefund(physicalCardRef.getAmt());
//        businessDailyReport.getScanCodeChannelFee()!=null?businessDailyReport.getScanCodeChannelFee():BigDecimal.ZERO
        BigDecimal arrivalAmt = (businessDailyReport.getArrivalAmount()!=null?businessDailyReport.getArrivalAmount():BigDecimal.ZERO)
                .add(settDto.getTotalWithdrawalAmount())
                .add(superPay.getWithdrawalAmount()!=null?superPay.getWithdrawalAmount():BigDecimal.ZERO).add(forSaleActivityWithdrawalAmount);
        businessDailyReport.setArrivalAmount(arrivalAmt);
        BigDecimal scanChannelFee =  settDto.getTotalServiceCharge().add((businessDailyReport.getScanCodeChannelFee()!=null?businessDailyReport.getScanCodeChannelFee():BigDecimal.ZERO)).add(superPay.getServiceCharge()!=null?superPay.getServiceCharge():BigDecimal.ZERO).add(forSaleActivityCom);
        businessDailyReport.setScanCodeChannelFee(scanChannelFee);
        businessDailyReport.setAddPoints(BigDecimal.valueOf(integral));
        businessDailyReport.setDeductPoints(BigDecimal.valueOf(writeOffIntegral));
        if(isClass == 1){
               classSettlementMapper.insertBusinessDailyClass(businessDailyReport);
        }else if(isClass == 2){
            classSettlementMapper.insertBusinessDailyClassEight(businessDailyReport);
        }else{
               classSettlementMapper.insertBusinessDailyReport(businessDailyReport);
        }

    }



    @Override
    public CardClassSettDto countCardSett(List<ClassSettlementCard> settlementCardList){
        Map<CardRecordType,ClassSettlementCard> map = new HashMap<>();
        // 根据payType进行分组统计
        for (ClassSettlementCard settlementCard : settlementCardList) {
            if (!org.apache.commons.lang3.StringUtils.equals("dzk",settlementCard.getRechargeType())) {
                continue;
            }
            CardRecordType payType = CardRecordType.getByKey(settlementCard.getPayType(),CardRecordType.CONSUME);
            ClassSettlementCard settlement = map.get(payType);
            if (settlement == null) {
                settlement = new ClassSettlementCard();
                settlement.setPayType(payType.getKey());
                settlement.setAmt(BigDecimal.ZERO);
                settlement.setOrderCount(0L);
                settlement.setServiceCharge(BigDecimal.ZERO);
                settlement.setGiveAmt(BigDecimal.ZERO);
                map.put(payType,settlement);
            }
            settlement.setAmt(MathUtil.add(settlement.getAmt(),settlementCard.getAmt()));
            settlement.setOrderCount(MathUtil.add(settlement.getOrderCount(),settlementCard.getOrderCount()));
            settlement.setServiceCharge(MathUtil.add(settlement.getServiceCharge(),settlementCard.getServiceCharge()==null?null:settlementCard.getServiceCharge().setScale(2,RoundingMode.HALF_UP)));
            settlement.setGiveAmt(MathUtil.add(settlement.getGiveAmt(),settlementCard.getGiveAmt()));
        }
        CardClassSettDto settDto = new CardClassSettDto();// 每个payType数据相加的总计
        settDto.setWxAmt(BigDecimal.ZERO);
        settDto.setPosAmt(BigDecimal.ZERO);
        settDto.setTotalWxServiceCharge(BigDecimal.ZERO);
        settDto.setTotalPosServiceCharge(BigDecimal.ZERO);
        settDto.setTotalAmt(BigDecimal.ZERO);
        settDto.setTotalRechargeAmt(BigDecimal.ZERO);
        settDto.setTotalGiveAmt(BigDecimal.ZERO);
        settDto.setTotalWithdrawalAmount(BigDecimal.ZERO);
        settDto.setTotalRefundAmt(BigDecimal.ZERO);
        settDto.setTotalServiceCharge(BigDecimal.ZERO);
        ArrayList<ClassSettlementCard> settList = new ArrayList<>(); // 包含所有payType数据的列表
        // 遍历所有的payType,添加map中没有的类型，并进行总计
        for (CardRecordType value : CardRecordType.values()) {
            ClassSettlementCard settlement = map.get(value);
            if (settlement == null) {
                settlement = new ClassSettlementCard();
                settlement.setPayType(value.getKey());
                settlement.setAmt(BigDecimal.ZERO);
                settlement.setOrderCount(0L);
                settlement.setServiceCharge(BigDecimal.ZERO);
                settlement.setGiveAmt(BigDecimal.ZERO);
                settlement.setWithdrawalAmount(BigDecimal.ZERO);
            }
            settList.add(settlement);
            settDto.setTotalOrderCount((int) (settDto.getTotalOrderCount()+ settlement.getOrderCount()));
            switch (value){
//                case MANUAL_ADD:
//                    settDto.setTotalAmt(settDto.getTotalAmt().add(settlement.getAmt()));
//                    break;
//                case MANUAL_SUB:
//                    settlement.setAmt(settlement.getAmt().negate());
//                    settDto.setTotalAmt(settDto.getTotalAmt().add(settlement.getAmt()));
//                    break;
                case WX_RECHARGE:
                    settDto.setTotalAmt(settDto.getTotalAmt().add(settlement.getAmt()));
                    settDto.setWxAmt(settDto.getWxAmt().add(settlement.getAmt()));
                    settDto.setTotalWxServiceCharge(settDto.getTotalWxServiceCharge().add(settlement.getServiceCharge()));
                    settDto.setAllAmt(settDto.getTotalAmt().add(settlement.getAmt()));
                    settDto.setTotalGiveAmt(settDto.getTotalGiveAmt().add(settlement.getGiveAmt()));
                    settlement.setWithdrawalAmount(settlement.getAmt().subtract(settlement.getServiceCharge())); // 充值提现=实收-渠道费
                    settDto.setTotalWithdrawalAmount(settDto.getTotalWithdrawalAmount().add(settlement.getWithdrawalAmount()));
                    settDto.setTotalRechargeAmt(settDto.getTotalRechargeAmt().add(settlement.getAmt()));
                    settDto.setTotalServiceCharge(settDto.getTotalServiceCharge().add(settlement.getServiceCharge()));
                    settDto.setAmt(settlement.getAmt());
                    settDto.setOrderCount(settlement.getOrderCount());
                    break;
                case MANUAL_ADD:
                    settDto.setAllAmt(settDto.getTotalAmt().add(settlement.getAmt()));

                    break;
                case MANUAL_SUB:
                    settDto.setAllAmt(settDto.getTotalAmt().subtract(settlement.getAmt()));
                    break;
//                case CONSUME:
//                    settlement.setAmt(settlement.getAmt().negate());
//                    settDto.setTotalAmt(settDto.getTotalAmt().add(settlement.getAmt()));
//                    break;
                case POS_RECHARGE:
                    settDto.setPosAmt(settDto.getPosAmt().add(settlement.getAmt()));
                    settDto.setTotalPosServiceCharge(settDto.getTotalPosServiceCharge().add(settlement.getServiceCharge()));
                    settDto.setTotalAmt(settDto.getTotalAmt().add(settlement.getAmt()));
                    settDto.setAllAmt(settDto.getTotalAmt().add(settlement.getAmt()));
                    settDto.setTotalGiveAmt(settDto.getTotalGiveAmt().add(settlement.getGiveAmt()));
                    settlement.setWithdrawalAmount(settlement.getAmt().subtract(settlement.getServiceCharge())); // 充值提现=实收-渠道费
                    settDto.setTotalWithdrawalAmount(settDto.getTotalWithdrawalAmount().add(settlement.getWithdrawalAmount()));
                    settDto.setTotalRechargeAmt(settDto.getTotalRechargeAmt().add(settlement.getAmt()));
                    settDto.setTotalServiceCharge(settDto.getTotalServiceCharge().add(settlement.getServiceCharge()));
                    settDto.setAmt(settlement.getAmt());
                    settDto.setOrderCount(settlement.getOrderCount());
                    break;
                case POS_REFUND:
                    settDto.setTotalAmt(settDto.getTotalAmt().subtract(settlement.getAmt()));
                    settDto.setPosAmt(settDto.getPosAmt().subtract(settlement.getAmt()));
                    settDto.setTotalPosServiceCharge(MathUtil.subtract(settDto.getTotalPosServiceCharge(),settlement.getServiceCharge()));
                    settDto.setAllAmt(settDto.getTotalAmt().subtract(settlement.getAmt()));
                    settDto.setTotalRefundAmt(settDto.getTotalRefundAmt().add(settlement.getAmt()));
                    settDto.setTotalGiveAmt(MathUtil.subtract(settDto.getTotalGiveAmt(),settlement.getGiveAmt()));
                    settlement.setWithdrawalAmount(settlement.getServiceCharge().subtract(settlement.getAmt())); // 退款提现=-(实收-渠道费)，总提现要加上这个值
                    settDto.setTotalWithdrawalAmount(settDto.getTotalWithdrawalAmount().add(settlement.getWithdrawalAmount()));
                    settDto.setTotalServiceCharge(MathUtil.subtract(settDto.getTotalServiceCharge(),settlement.getServiceCharge()));
                    break;
                case REFUND:
                    settDto.setTotalAmt(settDto.getTotalAmt().subtract(settlement.getAmt()));
                    settDto.setWxAmt(settDto.getWxAmt().subtract(settlement.getAmt()));
                    settDto.setTotalWxServiceCharge(MathUtil.subtract(settDto.getTotalWxServiceCharge(),settlement.getServiceCharge()));
                    settDto.setAllAmt(settDto.getTotalAmt().subtract(settlement.getAmt()));
                    settDto.setTotalRefundAmt(settDto.getTotalRefundAmt().add(settlement.getAmt()));
                    settDto.setTotalGiveAmt(MathUtil.subtract(settDto.getTotalGiveAmt(),settlement.getGiveAmt()));
                    settlement.setWithdrawalAmount(settlement.getServiceCharge().subtract(settlement.getAmt())); // 退款提现=-(实收-渠道费)，总提现要加上这个值
                    settDto.setTotalWithdrawalAmount(settDto.getTotalWithdrawalAmount().add(settlement.getWithdrawalAmount()));
                    settDto.setTotalServiceCharge(MathUtil.subtract(settDto.getTotalServiceCharge(),settlement.getServiceCharge()));
                    //settlement.setAmt(settlement.getAmt().negate());
                    break;
            }
        }
//        ClassSettlementCard countDto = new ClassSettlementCard();
//        countDto.setPayType("zj");
//        countDto.setAmt(settDto.getTotalAmt());
//        countDto.setOrderCount((long)settDto.getOrderCount());
//        countDto.setWithdrawalAmount(settDto.getTotalWithdrawalAmount());
//        settList.add(countDto);
        settDto.setSettlementCardList(settList);
        return settDto;
    }


    /**
     * 班结分组关系到充值
     *
     * @param
     * @return 结果
     */
    @Override
    public List<ClassSettlementCard> queryGroupByRecharge(List<Map<String, Object>> queryGroupByPayType,CardClassSettDto settDto) {
        List<ClassSettlementCard> queryGroupByRecharge = new ArrayList<ClassSettlementCard>();
        ClassSettlementCard dkqClassSettlementCard = new ClassSettlementCard();
        dkqClassSettlementCard.setPayType("dkq");
        dkqClassSettlementCard.setGiveAmt(BigDecimal.ZERO);
        dkqClassSettlementCard.setAmt(BigDecimal.ZERO);
        dkqClassSettlementCard.setOrderCount(0L);
        Map<String, Object> dzkCus = null;
        if (queryGroupByPayType != null && !queryGroupByPayType.isEmpty()) {
            for (Map<String, Object> obj : queryGroupByPayType) {
                if (obj.containsKey("pay_type") && obj.get("pay_type").equals("dkq")) {
                    dkqClassSettlementCard.setGiveAmt(BigDecimal.ZERO);
                    double amtDouble = (obj.get("amt") != null) ? (double) obj.get("amt") : 0.0;
                    BigDecimal amt = BigDecimal.valueOf(amtDouble);
                    dkqClassSettlementCard.setAmt(amt); // 强制类型转换
                    Long order_count = (obj.get("order_count") != null) ? ((BigDecimal) obj.get("order_count")).longValue() : 0L;
                    dkqClassSettlementCard.setOrderCount(order_count); // 强制类型转换
                }
                if (obj.containsKey("pay_type") && obj.get("pay_type").equals("dzk")) {
                    dzkCus = obj;
                }
            }
        }
        queryGroupByRecharge.add(dkqClassSettlementCard);
        List<ClassSettlementCard> classSettlementDZK = settDto.getSettlementCardList();
        for(ClassSettlementCard classSettlementDZKItem:classSettlementDZK){
            if(classSettlementDZKItem.getPayType().equals("msub")){
                ClassSettlementCard newClassSettlementCard = new ClassSettlementCard();
                newClassSettlementCard.setPayType("msub");
                newClassSettlementCard.setGiveAmt(BigDecimal.ZERO);
                newClassSettlementCard.setAmt(MathUtil.add(classSettlementDZKItem.getAmt(),classSettlementDZKItem.getGiveAmt()));
                newClassSettlementCard.setOrderCount(classSettlementDZKItem.getOrderCount());
                queryGroupByRecharge.add(newClassSettlementCard);
            }
//            else if(classSettlementDZKItem.getPayType().equals("")){
//                ClassSettlementCard newClassSettlementCard = new ClassSettlementCard();
//                newClassSettlementCard.setPayType("");
//                newClassSettlementCard.setGiveAmt(classSettlementDZKItem.getGiveAmt());
//                newClassSettlementCard.setAmt(classSettlementDZKItem.getAmt());
//                newClassSettlementCard.setOrderCount(classSettlementDZKItem.getOrderCount());
//                queryGroupByRecharge.add(newClassSettlementCard);
//            }
        }
        if (dzkCus != null) {
            ClassSettlementCard newClassSettlementCard = new ClassSettlementCard();
            newClassSettlementCard.setPayType("");
            double discountAmtDouble = (dzkCus.get("discount_amt") != null) ? (double) dzkCus.get("discount_amt") : 0d;
            double discountCouponAmtDouble = (dzkCus.get("discount_coupon_amt") != null) ? (double) dzkCus.get("discount_coupon_amt") : 0d;
            newClassSettlementCard.setGiveAmt(BigDecimal.valueOf(discountAmtDouble).add(BigDecimal.valueOf(discountCouponAmtDouble)));
            newClassSettlementCard.setAmt((dzkCus.get("amt") != null) ? BigDecimal.valueOf((double) dzkCus.get("amt")) : BigDecimal.ZERO);
            newClassSettlementCard.setOrderCount((dzkCus.get("order_count") != null) ? ((BigDecimal) dzkCus.get("order_count")).longValue() : 0L);
            queryGroupByRecharge.add(newClassSettlementCard);
        }
        return queryGroupByRecharge;
    }
    /**
     * 修改班结
     *
     * @param classSettlement 班结
     * @return 结果
     */
    @Override
    public int updateClassSettlement(ClassSettlement classSettlement) {
        return classSettlementMapper.updateClassSettlement(classSettlement);
    }

    /**
     * 批量删除班结
     *
     * @param classNo 需要删除的班结ID
     * @return 结果
     */
    @Override
    public int deleteClassSettlementByClassNos(String[] classNo) {
        classSettlementCardMapper.deleteClassSettlementByClassNos(classNo);
        classSettlementCardMapper.deleteOilByClassNos(classNo);
        classSettlementCardMapper.deleteReportByClassNos(classNo);
        classSettlementCardMapper.deleteOilOmeterByClassNos(classNo);
        return classSettlementMapper.deleteClassSettlementByClassNos(classNo)+classSettlementMapper.deleteClassSettlementStaffByClassNos(classNo);
    }

    /**
     * 删除班结信息
     *
     * @param classSettlementId 班结ID
     * @return 结果
     */
    @Override
    public int deleteClassSettlementById(Long classSettlementId) {
        return classSettlementMapper.deleteClassSettlementById(classSettlementId);
    }


    // 根据station_id修改白班时间
    @Override
    public int updateDayShiftTimeById(Time dayShiftStartTime, Time dayShiftEndTime, Time nightShiftStartTime, Time nightShiftEndTime, StationClassTimeDto timeDto){
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysUser user = currentUser.getUser();

        if (user.getUserLevel() == null) {
            return 0;
        } else if (user.getUserLevel() != null && user.getUserLevel().equals("1")) {
            int a = 0;
            Map<String, Object> params = new HashMap<>();
            params.put("enterpriseId", user.getEnterpriseId().intValue());
            params.put("dayShiftStartTime", dayShiftStartTime);
            params.put("dayShiftEndTime", dayShiftEndTime);
            params.put("nightShiftStartTime", nightShiftStartTime);
            params.put("nightShiftEndTime", nightShiftEndTime);
            params.put("dayShiftUserIds", timeDto.getDayShiftUserIds());
            params.put("nightShiftUserIds", timeDto.getNightShiftUserIds());
            a= a+classSettlementMapper.updateDayShiftTimeByEnterpriseId(params);
            Station station = new Station();
            station.setEnterpriseId(user.getEnterpriseId());
            List<Station> stations = stationMapper.selectStationList(station);
            for (Station ss : stations) {
                params.put("stationId", ss.getStationId().intValue());
                a= a+ classSettlementMapper.updateDayShiftTimeByStationId(params);
            }
            return a;

        } else if (user.getUserLevel().equals("2")) {
            Map<String, Object> params = new HashMap<>();
            params.put("stationId", user.getStationId().intValue());
            params.put("dayShiftStartTime", dayShiftStartTime);
            params.put("dayShiftEndTime", dayShiftEndTime);
            params.put("nightShiftStartTime", nightShiftStartTime);
            params.put("nightShiftEndTime", nightShiftEndTime);
            params.put("dayShiftUserIds", timeDto.getDayShiftUserIds());
            params.put("nightShiftUserIds", timeDto.getNightShiftUserIds());
            return classSettlementMapper.updateDayShiftTimeByStationId(params);
        }else{
            return 0;
        }
    };


    @Override
    public StationClassTimeVo selectDayShiftTimeById(){
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysUser user = currentUser.getUser();

        if (user.getUserLevel() == null) {
            return null;
        } else if (user.getUserLevel() != null && user.getUserLevel().equals("1")) {
            return classSettlementMapper.selectDayShiftTimeByEnterpriseId(user.getEnterpriseId().intValue());
        } else if (user.getUserLevel().equals("2")) {
            return classSettlementMapper.selectDayShiftTimeByStationId(user.getStationId().intValue());
        } else {
            return null;
        }
    }



    /**
     * 查询未班结信息
     *
     * @param params
     * @return 结果
     */
    @Override
    public List<ClassSettlement> querySummaryByClassNoForOrder(Map<String, Object> params) {
        return classSettlementMapper.querySummaryByClassNoForOrder(params);
    }

    @Override
    public List<ClassSettlement> employeeQuerySummaryByClassNoForOrder(Map<String, Object> params) {
        return classSettlementMapper.employeeQuerySummaryByClassNoForOrder(params);
    }

    @Override
    public List<ClassSettlement> queryUntreatedData(Map<String, Object> params) {
        return classSettlementMapper.queryUntreatedData(params);
    }

    @Override
    public List<CouponSummary> queryCouponSum(Map<String, Object> params) {
        List<CouponSummary> couponSummaryA = classSettlementMapper.queryCouponSum(params);

        boolean hasCashCoupon = false;
        boolean hasCarWashCoupon = false;
        boolean hasLJCoupon = false; // Add boolean for "立减券"
        boolean hasZKCoupon = false; // Add boolean for "折扣券"

        for (CouponSummary couponSummary : couponSummaryA) {
            if ("现金券".equals(couponSummary.getCouponType())) {
                hasCashCoupon = true;
            }
            if ("洗车券".equals(couponSummary.getCouponType())) {
                hasCarWashCoupon = true;
            }
            if ("立减券".equals(couponSummary.getCouponType())) { // Check for "立减券"
                hasLJCoupon = true;
            }
            if ("折扣券".equals(couponSummary.getCouponType())) { // Check for "折扣券"
                hasZKCoupon = true;
            }
        }

        if (!hasCashCoupon) {
            CouponSummary cashCoupon = new CouponSummary();
            cashCoupon.setCouponType("现金券");
            cashCoupon.setCouponAmt(BigDecimal.ZERO);
            cashCoupon.setCount(0);
            couponSummaryA.add(cashCoupon);
        }

        if (!hasCarWashCoupon) {
            CouponSummary carWashCoupon = new CouponSummary();
            carWashCoupon.setCouponType("洗车券");
            carWashCoupon.setCouponAmt(BigDecimal.ZERO);
            carWashCoupon.setCount(0);
            couponSummaryA.add(carWashCoupon);
        }
        // Similar logic for adding "立减券" if not present
        if (!hasLJCoupon) {
            CouponSummary ljCoupon = new CouponSummary();
            ljCoupon.setCouponType("立减券");
            ljCoupon.setCouponAmt(BigDecimal.ZERO);
            ljCoupon.setCount(0);
            couponSummaryA.add(ljCoupon);
        }

        // Similar logic for adding "折扣券" if not present
        if (!hasZKCoupon) {
            CouponSummary zkCoupon = new CouponSummary();
            zkCoupon.setCouponType("折扣券");
            zkCoupon.setCouponAmt(BigDecimal.ZERO);
            zkCoupon.setCount(0);
            couponSummaryA.add(zkCoupon);
        }

        if(params !=null && params.get("openStaffClassInStation") == null){
            List<CouponSummary>  couponSummaryB = classSettlementMapper.queryCouponSumByCouponUser(params);
            for(CouponSummary couponSummaryItem :couponSummaryB){
                if (couponSummaryItem.getCouponType().equals("现金券")) {
                    for (CouponSummary summaryA : couponSummaryA) {
                        if (summaryA.getCouponType().equals("现金券")) {
                            summaryA.setCouponAmt(couponSummaryItem.getCouponAmt().add(summaryA.getCouponAmt()));
                            summaryA.setCount(couponSummaryItem.getCount() + summaryA.getCount());
                        }
                    }
                }
                if (couponSummaryItem.getCouponType().equals("洗车券")) {
                    for (CouponSummary summaryA : couponSummaryA) {
                        if (summaryA.getCouponType().equals("洗车券")) {
                            summaryA.setCouponAmt(couponSummaryItem.getCouponAmt().add(summaryA.getCouponAmt()));
                            summaryA.setCount(couponSummaryItem.getCount() + summaryA.getCount());
                        }
                    }
                }
                if (couponSummaryItem.getCouponType().equals("立减券")) {
                    for (CouponSummary summaryA : couponSummaryA) {
                        if (summaryA.getCouponType().equals("立减券")) {
                            summaryA.setCouponAmt(couponSummaryItem.getCouponAmt().add(summaryA.getCouponAmt()));
                            summaryA.setCount(couponSummaryItem.getCount() + summaryA.getCount());
                        }
                    }
                }
                if (couponSummaryItem.getCouponType().equals("折扣券")) {
                    for (CouponSummary summaryA : couponSummaryA) {
                        if (summaryA.getCouponType().equals("折扣券")) {
                            summaryA.setCouponAmt(couponSummaryItem.getCouponAmt().add(summaryA.getCouponAmt()));
                            summaryA.setCount(couponSummaryItem.getCount() + summaryA.getCount());
                        }
                    }
                }

            }

        }
        return couponSummaryA;
    }




    @Override
    public void selectChange4User4POSListForOrder(Map<String, Object> params) {
        List<changeClassSettlement4User> changeClassSettlement4User=classSettlementMapper.selectChange4User4POSListForOrder(params);
        for (changeClassSettlement4User changeClassSettlement :changeClassSettlement4User){
            changeClassSettlement.setPresentAmount(BigDecimal.ZERO);
            changeClassSettlement.setTotalDiscountAmt(BigDecimal.ZERO);
            changeClassSettlement.setReceivableAmt(changeClassSettlement.getAmt());
            changeClassSettlement.setClassNo( params.get("no").toString());
            classSettlementMapper.addChange4User4POSListForOrder(changeClassSettlement);
        }

    }

    @Override
    public  List<changeClassSettlement4User> selectChange4User4POSListForOrderStaff(Map<String, Object> params) {

        List<changeClassSettlement4User> changeClassSettlement4User = classSettlementMapper.selectChange4User4POSListForOrder(params);
        changeClassSettlement4User allChangeForUser = new changeClassSettlement4User();
        allChangeForUser.setOilPersonnelName("合计");
        allChangeForUser.setOrderCount(0);
        allChangeForUser.setAmt(BigDecimal.ZERO);
        for (changeClassSettlement4User change4User : changeClassSettlement4User) {
            allChangeForUser.setOrderCount(allChangeForUser.getOrderCount() + change4User.getOrderCount());
            allChangeForUser.setAmt(allChangeForUser.getAmt().add(change4User.getAmt()));
        }
        changeClassSettlement4User.add(allChangeForUser);
return changeClassSettlement4User;
    }


    @Override
    public  List<changeClassSettlement4User> selectAllChangeForOrderStaff(Map<String, Object> params) {

        List<changeClassSettlement4User> changeClassSettlement4User = classSettlementMapper.selectAllChangeForOrderStaff(params);
        changeClassSettlement4User allChangeForUser = new changeClassSettlement4User();
        allChangeForUser.setOilPersonnelName("合计");
        allChangeForUser.setOrderCount(0);
        allChangeForUser.setAmt(BigDecimal.ZERO);
        for (changeClassSettlement4User change4User : changeClassSettlement4User) {

            allChangeForUser.setOrderCount(allChangeForUser.getOrderCount() + change4User.getOrderCount());
            allChangeForUser.setAmt(allChangeForUser.getAmt().add(change4User.getAmt()));
        }
        changeClassSettlement4User.add(allChangeForUser);
        return changeClassSettlement4User;
    }

    @Override
    public void insertAboutOil(Map<String, Object> params,String no,String classEndTime,Long stationId) {
        List<Map<String, Object>> queryGroupByOilGun = this.queryGroupByOilGunForOrder(params);
        Iterator<Map<String, Object>> iterator2 = queryGroupByOilGun.iterator();
        while (iterator2.hasNext()) {
            Map<String, Object> map = iterator2.next();
            if (!map.containsKey("oil_gun")) {
                iterator2.remove();
            }
        }
        List<OilEngineDto> newQueryGroupByOilGun = classSettlementMapper.classSettlementOilEngineSum(params);
        List<String> oilGunListA = new ArrayList<>();
        List<String> oilGunListB = new ArrayList<>();
        if (queryGroupByOilGun.size() > 0 && newQueryGroupByOilGun.size() > 0) {
            for (Map<String, Object> map : queryGroupByOilGun) {
                String oilGun = map.get("oil_gun").toString();
                map.put("class_no",no);
                map.put("class_end_time",classEndTime);
                map.put("station_id",stationId);
                oilGunListA.add(oilGun); // 存储a列表的油枪数据
                oilGunListB.clear();
                for (OilEngineDto oilEngineDto : newQueryGroupByOilGun) {
                    oilGunListB.add(oilEngineDto.getOilGun());
                    if (oilEngineDto != null && oilEngineDto.getOilGun() != null && oilEngineDto.getOilGun().equals(oilGun)) {
                        map.put("oil_price", oilEngineDto.getOilPrice());
                        map.put("class_no",no);
                        map.put("class_end_time",classEndTime);
                        map.put("station_id",stationId);
                        map.put("order_liters", oilEngineDto.getOrderLiters());
                        map.put("oil_name", oilEngineDto.getOilName());
                        map.put("startFlowMeter", oilEngineDto.getStartFlowMeter());
                        map.put("endFlowMeter", oilEngineDto.getEndFlowMeter());
                        map.put("totalWordCount", 0);
                        // 获取 startFlowMeter 和 endFlowMeter 的值
                        Double startFlowMeterValue = oilEngineDto.getStartFlowMeter();
                        Double endFlowMeterValue = oilEngineDto.getEndFlowMeter();
                        // 空值检查
                        if (startFlowMeterValue != null && endFlowMeterValue != null) {
                            // 计算差值
                            Double diff = endFlowMeterValue - startFlowMeterValue;

                            // 将差值放入 map 中
                            map.put("totalWordCount", diff);
                        }
                    }
                }
            }
            List<String> difference = new ArrayList<>();
            for (String value : oilGunListB) {
                if (!oilGunListA.contains(value)) {
                    difference.add(value);
                }
            }
            for (OilEngineDto oilEngineDto : newQueryGroupByOilGun) {
                for (String oilGun : difference) {

                    if (oilEngineDto != null && oilEngineDto.getOilGun() != null && oilEngineDto.getOilGun().equals(oilGun)) {

                        Map<String, Object> map = new HashMap<>();
                        map.put("class_no",no);
                        map.put("class_end_time",classEndTime);
                        map.put("station_id",stationId);
                        map.put("oil_gun", oilEngineDto.getOilGun());
                        map.put("oil_price", oilEngineDto.getOilPrice());
                        map.put("order_liters", oilEngineDto.getOrderLiters());
                        map.put("oil_name", oilEngineDto.getOilName());
                        map.put("startFlowMeter", oilEngineDto.getStartFlowMeter());
                        map.put("endFlowMeter", oilEngineDto.getEndFlowMeter());
                        map.put("totalWordCount", 0);
                        // 获取 startFlowMeter 和 endFlowMeter 的值
                        Double startFlowMeterValue = oilEngineDto.getStartFlowMeter();
                        Double endFlowMeterValue = oilEngineDto.getEndFlowMeter();
                        // 空值检查
                        if (startFlowMeterValue != null && endFlowMeterValue != null) {
                            // 计算差值
                            Double diff = endFlowMeterValue - startFlowMeterValue;

                            // 将差值放入 map 中
                            map.put("totalWordCount", diff);
                        }
                        queryGroupByOilGun.add(map);
                    }
                }
            }

        } else if (queryGroupByOilGun.size() == 0 && newQueryGroupByOilGun.size() > 0) {
            for (OilEngineDto oilEngineDto : newQueryGroupByOilGun) {
                if (oilEngineDto != null && oilEngineDto.getOilGun() != null) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("class_no",no);
                    map.put("class_end_time",classEndTime);
                    map.put("station_id",stationId);
                    map.put("oil_gun", oilEngineDto.getOilGun());
                    map.put("oil_price", oilEngineDto.getOilPrice());
                    map.put("order_liters", oilEngineDto.getOrderLiters());
                    map.put("oil_name", oilEngineDto.getOilName());
                    map.put("startFlowMeter", oilEngineDto.getStartFlowMeter());
                    map.put("endFlowMeter", oilEngineDto.getEndFlowMeter());
                    map.put("totalWordCount", 0);
                    // 获取 startFlowMeter 和 endFlowMeter 的值
                    Double startFlowMeterValue = oilEngineDto.getStartFlowMeter();
                    Double endFlowMeterValue = oilEngineDto.getEndFlowMeter();
                    // 空值检查
                    if (startFlowMeterValue != null && endFlowMeterValue != null) {
                        // 计算差值
                        Double diff = endFlowMeterValue - startFlowMeterValue;

                        // 将差值放入 map 中
                        map.put("totalWordCount", diff);
                    }
                    queryGroupByOilGun.add(map);

                }
            }
        }
        List<OilOmeter> oilOmeters =  classSettlementMapper.selectByStationId(stationId);
        for (OilOmeter oilOmeter:oilOmeters){
            oilOmeter.setClassEndTime(classEndTime);
            oilOmeter.setClassNo(no);
            classSettlementMapper.insertSelective(oilOmeter);
        }
        if(queryGroupByOilGun.size()!=0){
            classSettlementMapper.insertAboutOilEngine(queryGroupByOilGun);
        }
    }

    @Override
    public List<changeClassSettlement4User> selectForRecharge(Map<String,Object> params) {
        List<changeClassSettlement4User> changeClassSettlement4User =  classSettlementMapper.select4UserListForOrder(params);
        changeClassSettlement4User allChangeForUser = new changeClassSettlement4User();
        allChangeForUser.setOilPersonnelName("合计");
        allChangeForUser.setOrderCount(0);
        allChangeForUser.setAmt(BigDecimal.ZERO);
        for(changeClassSettlement4User change4User:changeClassSettlement4User){
            allChangeForUser.setOrderCount(allChangeForUser.getOrderCount()+change4User.getOrderCount());
            allChangeForUser.setAmt(allChangeForUser.getAmt().add(change4User.getAmt()));
        }
        changeClassSettlement4User.add(allChangeForUser);
        return changeClassSettlement4User;
    }

    @Override
    public List<ClassSettlement> employeeQueryUntreatedData(Map<String, Object> params) {
        return classSettlementMapper.employeeQueryUntreatedData(params);
    }

    @Override
    public ClassSettlementCard queryPhysicalCard(Map<String, Object> params) {
        return classSettlementMapper.queryPhysicalCard(params);
    }

    @Override
    public ClassSettlementCard queryPhysicalCardRef(Map<String, Object> params) {
        return classSettlementMapper.queryPhysicalCardRef(params);
    }


    @Override
    public ClassSettlementCard queryFleetCard(Map<String, Object> params) {
        return classSettlementMapper.queryFleetCard(params);
    }

    @Override
    public ClassSettlementCard querySuperPay(Map<String, Object> params) {
        ClassSettlementCard superRef =   classSettlementMapper.queryRefundSuperPay(params);
        ClassSettlementCard superAmt =  classSettlementMapper.querySuperPay(params)!=null?
                classSettlementMapper.querySuperPay(params):new ClassSettlementCard();
        BigDecimal superRefAmt = superRef.getAmt()!=null?superRef.getAmt():BigDecimal.ZERO;
        superAmt.setRefundAmount(superRefAmt);
        return superAmt;
    }

    @Override
    public BigDecimal forKillCouponAmt(Map<String, Object> params) {
        BigDecimal forKillCouponAmt = BigDecimal.ZERO;
        ClassSettlementCard forKillCoupon = classSettlementMapper.forKillCouponAmt(params);
        forKillCouponAmt = forKillCoupon.getAmt()!=null?forKillCoupon.getAmt():BigDecimal.ZERO;
        return forKillCouponAmt;
    }

    @Override
    public BigDecimal forSaleActivityAmt(Map<String, Object> params) {
        ClassSettlementCard forSaleActivity = classSettlementMapper.forSaleActivityAmt(params);
        BigDecimal forSaleActivityAmt   = forSaleActivity.getWithdrawalAmount()!=null?forSaleActivity.getWithdrawalAmount():BigDecimal.ZERO;
        return forSaleActivityAmt;
    }

    @Override
    public ClassSettlementCard forSaleActivity(Map<String, Object> params) {
        ClassSettlementCard forSaleActivity = classSettlementMapper.forSaleActivityAmt(params) !=null?classSettlementMapper.forSaleActivityAmt(params):new ClassSettlementCard();

        return forSaleActivity;
    }

    @Override
    public BigDecimal forSaleActivityCom(Map<String, Object> params) {
        ClassSettlementCard forSaleActivity = classSettlementMapper.forSaleActivityAmt(params);
        BigDecimal forSaleActivityCom   = forSaleActivity.getServiceCharge()!=null?forSaleActivity.getServiceCharge():BigDecimal.ZERO;
        return forSaleActivityCom;
    }

    @Override
    public BigDecimal refSaleActivityAmt(Map<String, Object> params) {
        ClassSettlementCard forSaleActivity =   classSettlementMapper.refSaleActivityAmt(params);
        BigDecimal refSaleActivityAmt   = forSaleActivity.getAmt()!=null?forSaleActivity.getAmt():BigDecimal.ZERO;
        return refSaleActivityAmt;
    }



    @Override
    public BigDecimal employeeForKillCouponAmt(Map<String, Object> params) {
        BigDecimal forKillCouponAmt = BigDecimal.ZERO;
        ClassSettlementCard forKillCoupon = classSettlementMapper.employeeForKillCouponAmt(params);
        forKillCouponAmt = forKillCoupon.getAmt();
        return forKillCouponAmt;
    }

    @Override
    public BigDecimal forActualRechargeReceivedAmt(CardClassSettDto settDto, Map<String, Object> params) {
        BigDecimal actualRechargeReceivedAmt =  settDto.getAllAmt();
        actualRechargeReceivedAmt = (actualRechargeReceivedAmt != null) ? actualRechargeReceivedAmt : BigDecimal.ZERO;
        ClassSettlementCard physicalCard = classSettlementMapper.queryPhysicalCard(params);
        ClassSettlementCard fleetCard = classSettlementMapper.queryFleetCard(params);
        ClassSettlementCard superPay = classSettlementMapper.querySuperPay(params);
        if (actualRechargeReceivedAmt != null) {
            BigDecimal physicalCardAmt = physicalCard.getAmt() != null ? physicalCard.getAmt() : BigDecimal.ZERO;
            BigDecimal fleetCardAmt = fleetCard.getAmt() != null ? fleetCard.getAmt() : BigDecimal.ZERO;
            BigDecimal superPayAmt = superPay.getAmt() != null ? superPay.getAmt() : BigDecimal.ZERO;
            actualRechargeReceivedAmt = actualRechargeReceivedAmt.add(physicalCardAmt).add(fleetCardAmt).add(superPayAmt);

        }
        return actualRechargeReceivedAmt;
    }

    @Override
    public BigDecimal homeRechargeReceivedAmt( Map<String, Object> params) {
        BigDecimal  actualRechargeReceivedAmt = BigDecimal.ZERO;
        ClassSettlementCard physicalCard = classSettlementMapper.queryPhysicalCard(params);
        ClassSettlementCard refPhysicalCard = classSettlementMapper.queryPhysicalCardRef(params);
        ClassSettlementCard fleetCard = classSettlementMapper.queryFleetCard(params);
        ClassSettlementCard superPay = classSettlementMapper.querySuperPay(params);
        ClassSettlementCard dzkCard = classSettlementMapper.queryDzkCard(params);
        BigDecimal refPhysicalCardAmt = refPhysicalCard.getAmt() != null ? refPhysicalCard.getAmt() : BigDecimal.ZERO;
            BigDecimal physicalCardAmt = physicalCard.getAmt() != null ? physicalCard.getAmt() : BigDecimal.ZERO;
            BigDecimal fleetCardAmt = fleetCard.getAmt() != null ? fleetCard.getAmt() : BigDecimal.ZERO;
        BigDecimal dzkAmt = BigDecimal.ZERO;
        BigDecimal refDzkAmt =  BigDecimal.ZERO;
          if(dzkCard!=null){
               dzkAmt = dzkCard.getAmt() != null ? dzkCard.getAmt() : BigDecimal.ZERO;
               refDzkAmt = dzkCard.getRefundAmount() != null ? dzkCard.getRefundAmount() : BigDecimal.ZERO;
          }
        BigDecimal superPayAmt = superPay.getAmt() != null ? superPay.getAmt() : BigDecimal.ZERO;
            actualRechargeReceivedAmt = actualRechargeReceivedAmt.add(physicalCardAmt)
                    .add(fleetCardAmt).add(superPayAmt)
                    .add(dzkAmt).subtract(refDzkAmt)
                    .subtract(refPhysicalCardAmt);
        return actualRechargeReceivedAmt;
    }

    @Override
    public List<ClassSettlementCard> homeRechargeReceivedAmtSeven( Map<String, Object> params) {
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        LocalDate comparedate1 = LocalDate.now().minusDays(7);
        LocalDate comparedate2 = LocalDate.now().minusDays(6);
        LocalDate comparedate3 = LocalDate.now().minusDays(5);
        LocalDate comparedate4 = LocalDate.now().minusDays(4);
        LocalDate comparedate5 = LocalDate.now().minusDays(3);
        LocalDate comparedate6 = LocalDate.now().minusDays(2);
        LocalDate comparedate7 = LocalDate.now().minusDays(1);
        List<LocalDate>  createdList = new ArrayList<>();
        params.put("createdList",createdList);
        createdList.add(comparedate1);
        createdList.add(comparedate2);
        createdList.add(comparedate3);
        createdList.add(comparedate4);
        createdList.add(comparedate5);
        createdList.add(comparedate6);
        createdList.add(comparedate7);

        BigDecimal paycount1 = BigDecimal.ZERO;
        BigDecimal paycount2 = BigDecimal.ZERO;
        BigDecimal paycount3 = BigDecimal.ZERO;
        BigDecimal paycount4 = BigDecimal.ZERO;
        BigDecimal paycount5 = BigDecimal.ZERO;
        BigDecimal paycount6 = BigDecimal.ZERO;
        BigDecimal paycount7 = BigDecimal.ZERO;

        BigDecimal  actualRechargeReceivedAmt = BigDecimal.ZERO;
        List<ClassSettlementCard> dzkCards = classSettlementMapper.homeDzkCard(params);
        List<ClassSettlementCard> physicalCards= classSettlementMapper.homePhysical(params);
        List<ClassSettlementCard> refPhysicalCards = classSettlementMapper.homePhysicalRef(params);
        List<ClassSettlementCard> fleetCards = classSettlementMapper.homeFleet(params);
        List<ClassSettlementCard> superPays = classSettlementMapper.homeSuperPay(params);
        for (ClassSettlementCard dzkCard : dzkCards) {
            LocalDate localDate = dzkCard.getCreateTime().toInstant().atZone(zoneId).toLocalDateTime().toLocalDate();
            BigDecimal amt2 =   dzkCard.getAmt() != null ? dzkCard.getAmt() : BigDecimal.ZERO;
            BigDecimal amt3 =  dzkCard.getRefundAmount() != null ? dzkCard.getRefundAmount() : BigDecimal.ZERO;
            if (localDate.equals(comparedate1)) {
                paycount1 = paycount1.add(amt2).subtract(amt3);
            }
            if (localDate.equals(comparedate2)) {
                paycount2 = paycount2.add(amt2).subtract(amt3);
            }
            if (localDate.equals(comparedate3)) {
                paycount3 = paycount3.add(amt2).subtract(amt3);
            }
            if (localDate.equals(comparedate4)) {
                paycount4 = paycount4.add(amt2).subtract(amt3);
            }
            if (localDate.equals(comparedate5)) {
                paycount5 = paycount5.add(amt2).subtract(amt3);
            }
            if (localDate.equals(comparedate6)) {
                paycount6 = paycount6.add(amt2).subtract(amt3);
            }
            if (localDate.equals(comparedate7)) {
                paycount7 = paycount7.add(amt2).subtract(amt3);
            }
        }
        for (ClassSettlementCard physicalCard : physicalCards) {
            LocalDate localDate = physicalCard.getCreateTime().toInstant().atZone(zoneId).toLocalDateTime().toLocalDate();
            BigDecimal amt2 =  physicalCard.getAmt() != null ? physicalCard.getAmt() : BigDecimal.ZERO;
            if (localDate.equals(comparedate1)) {
                paycount1 = paycount1.add(amt2);
            }
            if (localDate.equals(comparedate2)) {
                paycount2 = paycount2.add(amt2);
            }
            if (localDate.equals(comparedate3)) {
                paycount3 = paycount3.add(amt2);
            }
            if (localDate.equals(comparedate4)) {
                paycount4 = paycount4.add(amt2);
            }
            if (localDate.equals(comparedate5)) {
                paycount5 = paycount5.add(amt2);
            }
            if (localDate.equals(comparedate6)) {
                paycount6 = paycount6.add(amt2);
            }
            if (localDate.equals(comparedate7)) {
                paycount7 = paycount7.add(amt2);
            }
        }
        for (ClassSettlementCard refPhysicalCard : refPhysicalCards) {
            LocalDate localDate = refPhysicalCard.getCreateTime().toInstant().atZone(zoneId).toLocalDateTime().toLocalDate();
            BigDecimal amt2 = refPhysicalCard.getAmt() != null ? refPhysicalCard.getAmt() : BigDecimal.ZERO;
            if (localDate.equals(comparedate1)) {
                paycount1 = paycount1.subtract(amt2);
            }
            if (localDate.equals(comparedate2)) {
                paycount2 = paycount2.subtract(amt2);
            }
            if (localDate.equals(comparedate3)) {
                paycount3 = paycount3.subtract(amt2);
            }
            if (localDate.equals(comparedate4)) {
                paycount4 = paycount4.subtract(amt2);
            }
            if (localDate.equals(comparedate5)) {
                paycount5 = paycount5.subtract(amt2);
            }
            if (localDate.equals(comparedate6)) {
                paycount6 = paycount6.subtract(amt2);
            }
            if (localDate.equals(comparedate7)) {
                paycount7 = paycount7.subtract(amt2);
            }
        }
        for (ClassSettlementCard fleetCard : fleetCards) {
            LocalDate localDate = fleetCard.getCreateTime().toInstant().atZone(zoneId).toLocalDateTime().toLocalDate();
            BigDecimal amt2 =  fleetCard.getAmt() != null ? fleetCard.getAmt() : BigDecimal.ZERO;
            if (localDate.equals(comparedate1)) {
                paycount1 = paycount1.add(amt2);
            }
            if (localDate.equals(comparedate2)) {
                paycount2 = paycount2.add(amt2);
            }
            if (localDate.equals(comparedate3)) {
                paycount3 = paycount3.add(amt2);
            }
            if (localDate.equals(comparedate4)) {
                paycount4 = paycount4.add(amt2);
            }
            if (localDate.equals(comparedate5)) {
                paycount5 = paycount5.add(amt2);
            }
            if (localDate.equals(comparedate6)) {
                paycount6 = paycount6.add(amt2);
            }
            if (localDate.equals(comparedate7)) {
                paycount7 = paycount7.add(amt2);
            }
        }
        for (ClassSettlementCard superPay : superPays) {
            LocalDate localDate = superPay.getCreateTime().toInstant().atZone(zoneId).toLocalDateTime().toLocalDate();
            BigDecimal amt2 =  superPay.getAmt() != null ? superPay.getAmt() : BigDecimal.ZERO;
            if (localDate.equals(comparedate1)) {
                paycount1 = paycount1.add(amt2);
            }
            if (localDate.equals(comparedate2)) {
                paycount2 = paycount2.add(amt2);
            }
            if (localDate.equals(comparedate3)) {
                paycount3 = paycount3.add(amt2);
            }
            if (localDate.equals(comparedate4)) {
                paycount4 = paycount4.add(amt2);
            }
            if (localDate.equals(comparedate5)) {
                paycount5 = paycount5.add(amt2);
            }
            if (localDate.equals(comparedate6)) {
                paycount6 = paycount6.add(amt2);
            }
            if (localDate.equals(comparedate7)) {
                paycount7 = paycount7.add(amt2);
            }
        }
        List<ClassSettlementCard> allChange = new ArrayList<>();
        ClassSettlementCard allChangeItem= new ClassSettlementCard();
        ClassSettlementCard allChangeItem1 = new ClassSettlementCard();
        allChangeItem1.setAmt(paycount1);
        allChangeItem1.setCreateTime(Date.from(comparedate1.atStartOfDay(ZoneId.systemDefault()).toInstant()));
        allChange.add(allChangeItem1);
        ClassSettlementCard allChangeItem2 = new ClassSettlementCard();
        allChangeItem2.setAmt(paycount2);
        allChangeItem2.setCreateTime(Date.from(comparedate2.atStartOfDay(ZoneId.systemDefault()).toInstant()));
        allChange.add(allChangeItem2);

        ClassSettlementCard allChangeItem3 = new ClassSettlementCard();
        allChangeItem3.setAmt(paycount3);
        allChangeItem3.setCreateTime(Date.from(comparedate3.atStartOfDay(ZoneId.systemDefault()).toInstant()));
        allChange.add(allChangeItem3);

        ClassSettlementCard allChangeItem4 = new ClassSettlementCard();
        allChangeItem4.setAmt(paycount4);
        allChangeItem4.setCreateTime(Date.from(comparedate4.atStartOfDay(ZoneId.systemDefault()).toInstant()));
        allChange.add(allChangeItem4);

        ClassSettlementCard allChangeItem5 = new ClassSettlementCard();
        allChangeItem5.setAmt(paycount5);
        allChangeItem5.setCreateTime(Date.from(comparedate5.atStartOfDay(ZoneId.systemDefault()).toInstant()));
        allChange.add(allChangeItem5);

        ClassSettlementCard allChangeItem6 = new ClassSettlementCard();
        allChangeItem6.setAmt(paycount6);
        allChangeItem6.setCreateTime(Date.from(comparedate6.atStartOfDay(ZoneId.systemDefault()).toInstant()));
        allChange.add(allChangeItem6);

        ClassSettlementCard allChangeItem7 = new ClassSettlementCard();
        allChangeItem7.setAmt(paycount7);
        allChangeItem7.setCreateTime(Date.from(comparedate7.atStartOfDay(ZoneId.systemDefault()).toInstant()));
        allChange.add(allChangeItem7);


        return allChange;
    }

    @Override
    public Map<String, Object> queryLastEndTimeStaff(Map<String, Object> params) {
        if(params.get("oilPersonnelId").equals("0")){
            return classSettlementMapper.queryLastEndTime(params);
        }else{
            return classSettlementMapper.queryStaffLastEndTime(params);
        }

    }


    @Override
    public Map<String, Object> queryLastEndTime(Map<String, Object> params) {
        return classSettlementMapper.queryLastEndTime(params);
    }

    @Override
    public Map<String, Object> queryStartAndEnd(Map<String, Object> params) {
        return classSettlementMapper.queryStartAndEnd(params);
    }

    @Override
    public List<ClassSettlement> querySummaryByClassNo(Map<String, Object> params) {
        return classSettlementMapper.querySummaryByClassNo(params);

    }

    @Override
    public List<ClassSettlement> querySummaryByClassNoAllData(Map<String, Object> params, CardClassSettDto settDto) {
        List<ClassSettlement> summaryByClassNo = classSettlementMapper.querySummaryByClassNo(params);
//记录一下，总退款在确认班结就处理了
        BigDecimal actualRechargeReceivedAmt = settDto.getTotalRechargeAmt();

        BigDecimal totalRechargeWithdrawalAmount = settDto.getTotalWithdrawalAmount();
        BigDecimal totalRechargeRefundAmt = settDto.getTotalRefundAmt();

//        //        消费合计退款
//        summaryMap.put("consumptionRefundAmt", consumptionRefundAmt);
////        优惠金额
//        summaryMap.put("totalDiscountAmt", totalDiscountAmt);
////        充值合计提现
//        summaryMap.put("totalRechargeWithdrawalAmount", totalRechargeWithdrawalAmount);
////        充值合计退款
//        summaryMap.put("totalRechargeRefundAmt", totalRechargeRefundAmt);
////        到账总金额
//        summaryMap.put("allAmt", allAmt);

        BigDecimal allAmt = BigDecimal.ZERO;//到账总金额
        BigDecimal consumptionRefundAmt = BigDecimal.ZERO;//消费合计退款
        if (summaryByClassNo != null && !summaryByClassNo.isEmpty()) {
            ClassSettlement firstItem = summaryByClassNo.get(0);
            if (firstItem != null) {
                BigDecimal discountAmt = firstItem.getDiscountAmt()!=null?firstItem.getDiscountAmt(): BigDecimal.ZERO;
                BigDecimal superPayAmt = firstItem.getSuperPayAmt()!=null?firstItem.getSuperPayAmt(): BigDecimal.ZERO;
                BigDecimal discountCouponAmt = firstItem.getDiscountCouponAmt()!=null?firstItem.getDiscountCouponAmt(): BigDecimal.ZERO;
                BigDecimal bargainDiscountAmt = firstItem.getBargainDiscountAmt()!=null?firstItem.getBargainDiscountAmt(): BigDecimal.ZERO;

                BigDecimal actualTotalAmt = firstItem.getActualRechargeReceivedAmt()!=null?firstItem.getActualRechargeReceivedAmt(): BigDecimal.ZERO;
                BigDecimal yhkCommission = firstItem.getYhkCommission()!=null?firstItem.getYhkCommission(): BigDecimal.ZERO;
                BigDecimal otherCommission = firstItem.getOtherCommission()!=null?firstItem.getOtherCommission(): BigDecimal.ZERO;
                BigDecimal saleActivityAmt  = firstItem.getSaleActivityAmt();
                BigDecimal saleAmtCom =firstItem.getSaleActivityCom()!=null?firstItem.getSaleActivityCom():BigDecimal.ZERO;
                if (discountAmt != null && discountCouponAmt != null&& bargainDiscountAmt != null) {
                    BigDecimal totalDiscountAmt = discountAmt.add(discountCouponAmt).add(bargainDiscountAmt);
                    firstItem.setTotalDiscountAmt(totalDiscountAmt);
                }
                BigDecimal totalWithdrawalAmount = firstItem.getTotalWithdrawalAmount();
                BigDecimal aggregatedPayment =firstItem.getAggregatedPayment() != null ? firstItem.getAggregatedPayment() : BigDecimal.ZERO;
                BigDecimal amt = summaryByClassNo.get(0).getAmt();
                BigDecimal nodzkAmt = summaryByClassNo.get(0).getNodzkAmt();
                otherCommission=  otherCommission.add(settDto.getTotalServiceCharge()!= null ? settDto.getTotalServiceCharge() : BigDecimal.ZERO).add(saleAmtCom);
                allAmt = nodzkAmt != null ? nodzkAmt.add(actualTotalAmt != null ? actualTotalAmt : BigDecimal.ZERO) : BigDecimal.ZERO;
                firstItem.setAllAmt(allAmt);
                Station station1 = stationMapper.selectStationById(firstItem.getStationId());
                firstItem.setAggregatedPayment(aggregatedPayment.add(saleActivityAmt != null ? saleActivityAmt : BigDecimal.ZERO));
                firstItem.setOtherCommission(otherCommission.setScale(2, RoundingMode.HALF_UP));
                firstItem.setYhkCommission(yhkCommission.setScale(2, RoundingMode.HALF_UP));
                firstItem.setIsShowCommission(station1.getIsShowCommission());
                firstItem.setAllCommission(otherCommission.add(yhkCommission).setScale(2, RoundingMode.HALF_UP));
                firstItem.setTotalRechargeWithdrawalAmount(totalRechargeWithdrawalAmount);
                ClassSettlementCard physicalCardRef = classSettlementMapper.queryPhysicalCardRef(params);
//                totalRechargeRefundAmt.add(physicalCardRef.getAmt() != null?physicalCardRef.getAmt():BigDecimal.ZERO);
//                firstItem.setTotalRechargeRefundAmt(totalRechargeRefundAmt);
                firstItem.setTotalWithdrawalAmount(superPayAmt.add(totalWithdrawalAmount != null ? totalWithdrawalAmount : BigDecimal.ZERO).add(saleActivityAmt != null ? saleActivityAmt : BigDecimal.ZERO));

            }
        } else {

        }
        return summaryByClassNo;
    }

    @Override
    public List<Map<String, Object>> queryGroupByOilForOrder(Map<String, Object> params) {
        return classSettlementMapper.queryGroupByOilForOrder(params);
    }

    @Override
    public List<Map<String, Object>> employeeQueryGroupByOilForOrder(Map<String, Object> params) {
        return classSettlementMapper.employeeQueryGroupByOilForOrder(params);
    }

    @Override
    public List<Map<String, Object>> queryGroupByOil(Map<String, Object> params) {
        List<Map<String, Object>> queryGroupByOil = classSettlementMapper.queryGroupByOil(params);
        Iterator<Map<String, Object>> iterator = queryGroupByOil.iterator();
        while (iterator.hasNext()) {
            Map<String, Object> map = iterator.next();
            if (!map.containsKey("oil_name")) {
                iterator.remove();
            }
        }
        Map<String, Object> allOil = new HashMap<String, Object>();
        BigDecimal orderLiterSum = BigDecimal.ZERO;
        BigDecimal receivableAmtSum = BigDecimal.ZERO;
        BigDecimal amtSum = BigDecimal.ZERO;
        BigDecimal discountAmtSum = BigDecimal.ZERO;
        BigDecimal bargainDiscountAmt = BigDecimal.ZERO;
        BigDecimal discountCouponAmtSum = BigDecimal.ZERO;
        BigDecimal orderCountSum = BigDecimal.ZERO;
        for (Map<String, Object> itemOil : queryGroupByOil) {
            Object orderLiterObj = itemOil.get("order_liters");
            double orderLiterDouble = (orderLiterObj != null) ? (double) orderLiterObj : 0.0;
            BigDecimal orderLiter = BigDecimal.valueOf(orderLiterDouble);
            if (orderLiter != null) {
                orderLiterSum = orderLiterSum.add(orderLiter);
            }

            Object receivableAmtObj = itemOil.get("receivable_amt");
            double receivableAmtDouble = (receivableAmtObj != null) ? (double) receivableAmtObj : 0.0;
            BigDecimal receivableAmt = BigDecimal.valueOf(receivableAmtDouble);
            if (receivableAmt != null) {
                receivableAmtSum = receivableAmtSum.add(receivableAmt);
            }

            Object amtObj = itemOil.get("amt");
            double amtDouble = (amtObj != null) ? (double) amtObj : 0.0;
            BigDecimal amt = BigDecimal.valueOf(amtDouble);
            if (amt != null) {
                amtSum = amtSum.add(amt);
            }
            Object bargain_discount_amtObj = itemOil.get("bargain_discount_amt");
            BigDecimal bargain_discount_amt = (bargain_discount_amtObj != null) ? (BigDecimal) bargain_discount_amtObj : BigDecimal.ZERO;
            if (bargain_discount_amt != null) {
                bargainDiscountAmt = bargainDiscountAmt.add(bargain_discount_amt);
            }
            Object discountAmtObj = itemOil.get("discount_amt");
            double discountAmtDouble = (discountAmtObj != null) ? (double) discountAmtObj : 0.0;
            BigDecimal discountAmt = BigDecimal.valueOf(discountAmtDouble);
            if (discountAmt != null) {
                discountAmtSum = discountAmtSum.add(discountAmt);
            }

            Object discountCouponAmtObj = itemOil.get("discount_coupon_amt");
            double discountCouponAmtDouble = (discountCouponAmtObj != null) ? (double) discountCouponAmtObj : 0.0;
            BigDecimal discountCouponAmt = BigDecimal.valueOf(discountCouponAmtDouble);
            if (discountCouponAmt != null) {
                discountCouponAmtSum = discountCouponAmtSum.add(discountCouponAmt);
            }

            Object orderCountObj = itemOil.get("order_count");
            BigDecimal orderCount = (orderCountObj != null) ? (BigDecimal) orderCountObj : BigDecimal.ZERO;
            BigDecimal orderCountBigDecimal = orderCount;
            if (orderCountBigDecimal != null) {
                orderCountSum = orderCountSum.add(orderCountBigDecimal);
            }
        }
        allOil.put("oil_name", "合计");
        allOil.put("order_liters", orderLiterSum);
        allOil.put("receivable_amt", receivableAmtSum);
        allOil.put("amt", amtSum);
        allOil.put("discount_amt", discountAmtSum);
        allOil.put("bargain_discount_amt", bargainDiscountAmt);
        allOil.put("discount_coupon_amt", discountCouponAmtSum);
        allOil.put("order_count", orderCountSum);
        queryGroupByOil.add(allOil);
        return queryGroupByOil;
    }

    @Override
    public List<Map<String, Object>> queryReportByOil(Map<String, Object> params) {
        List<Map<String, Object>> queryGroupByOil = classSettlementMapper.queryReportByOil(params);

        Iterator<Map<String, Object>> iterator = queryGroupByOil.iterator();
        while (iterator.hasNext()) {
            Map<String, Object> map = iterator.next();
            if (!map.containsKey("oil_name")) {
                iterator.remove();
            }
        }
        Map<String, Object> allOil = new HashMap<String, Object>();
        BigDecimal orderLiterSum = BigDecimal.ZERO;
        BigDecimal receivableAmtSum = BigDecimal.ZERO;
        BigDecimal amtSum = BigDecimal.ZERO;
        BigDecimal discountAmtSum = BigDecimal.ZERO;
        BigDecimal discountCouponAmtSum = BigDecimal.ZERO;
        BigDecimal orderCountSum = BigDecimal.ZERO;
        BigDecimal actual_recharge_received_amt = BigDecimal.ZERO;

        for (Map<String, Object> itemOil : queryGroupByOil) {
            Object orderLiterObj = itemOil.get("order_liters");
            double orderLiterDouble = (orderLiterObj != null) ? (double) orderLiterObj : 0.0;
            BigDecimal orderLiter = BigDecimal.valueOf(orderLiterDouble);
            if (orderLiter != null) {
                orderLiterSum = orderLiterSum.add(orderLiter);
            }

            Object receivableAmtObj = itemOil.get("receivable_amt");
            double receivableAmtDouble = (receivableAmtObj != null) ? (double) receivableAmtObj : 0.0;
            BigDecimal receivableAmt = BigDecimal.valueOf(receivableAmtDouble);
            if (receivableAmt != null) {
                receivableAmtSum = receivableAmtSum.add(receivableAmt);
            }

            Object amtObj = itemOil.get("amt");
            double amtDouble = (amtObj != null) ? (double) amtObj : 0.0;
            BigDecimal amt = BigDecimal.valueOf(amtDouble);
            if (amt != null) {
                amtSum = amtSum.add(amt);
            }

            Object discountAmtObj = itemOil.get("discount_amt");
            double discountAmtDouble = (discountAmtObj != null) ? (double) discountAmtObj : 0.0;
            BigDecimal discountAmt = BigDecimal.valueOf(discountAmtDouble);
            if (discountAmt != null) {
                discountAmtSum = discountAmtSum.add(discountAmt);
            }

            Object discountCouponAmtObj = itemOil.get("discount_coupon_amt");
            double discountCouponAmtDouble = (discountCouponAmtObj != null) ? (double) discountCouponAmtObj : 0.0;
            BigDecimal discountCouponAmt = BigDecimal.valueOf(discountCouponAmtDouble);
            if (discountCouponAmt != null) {
                discountCouponAmtSum = discountCouponAmtSum.add(discountCouponAmt);
            }

            Object orderCountObj = itemOil.get("order_count");
            BigDecimal orderCount = (orderCountObj != null) ? (BigDecimal) orderCountObj : BigDecimal.ZERO;
            BigDecimal orderCountBigDecimal = orderCount;
            if (orderCountBigDecimal != null) {
                orderCountSum = orderCountSum.add(orderCountBigDecimal);
            }

            Object actual_recharge_received_amtObj = itemOil.get("actual_recharge_received_amt");
            BigDecimal actual_recharge_received_amtCount = (actual_recharge_received_amtObj != null) ? (BigDecimal) actual_recharge_received_amtObj : BigDecimal.ZERO;
            BigDecimal actual_recharge_received_amtBigDecimal = actual_recharge_received_amtCount;
            if (actual_recharge_received_amtBigDecimal != null) {
                actual_recharge_received_amt = actual_recharge_received_amt.add(actual_recharge_received_amtBigDecimal);
            }
        }
        allOil.put("oil_name", "小计");
        allOil.put("order_liters", orderLiterSum);
        allOil.put("receivable_amt", receivableAmtSum);
        allOil.put("amt", amtSum);
        allOil.put("discount_amt", discountAmtSum);
        allOil.put("discount_coupon_amt", discountCouponAmtSum);
        allOil.put("order_count", orderCountSum);
        queryGroupByOil.add(allOil);
        Map<String, Object> changeOil = new HashMap<String, Object>();
        allOil.put("oil_name", "充值");
        allOil.put("order_liters", 0);
        allOil.put("receivable_amt", actual_recharge_received_amt);
        allOil.put("amt", actual_recharge_received_amt);
        allOil.put("discount_amt", 0);
        allOil.put("discount_coupon_amt", 0);
        allOil.put("order_count", 0);
        queryGroupByOil.add(changeOil);
        Map<String, Object> allallOil = new HashMap<String, Object>();
        allOil.put("oil_name", "合计");
        allOil.put("order_liters", 0);
        allOil.put("receivable_amt", receivableAmtSum.add(actual_recharge_received_amt));
        allOil.put("amt", amtSum.add(actual_recharge_received_amt));
        allOil.put("discount_amt", discountAmtSum);
        allOil.put("discount_coupon_amt", discountCouponAmtSum);
        allOil.put("order_count", 0);
        Iterator<Map<String, Object>> iteraoor = queryGroupByOil.iterator();
        while (iteraoor.hasNext()) {
            Map<String, Object> map = iteraoor.next();
            if (!map.containsKey("oil_name")) {
                iteraoor.remove();
            }
        }
        return queryGroupByOil;
    }

    @Override
    public List<Map<String, Object>> queryGroupByPayTypeForOrder(Map<String, Object> params) {
        return classSettlementMapper.queryGroupByPayTypeForOrder(params);
    }

    @Override
    public List<Map<String, Object>> getShiftHandover(Map<String, Object> params) {
        List<Map<String, Object>> queryClassByNo = classSettlementMapper.queryReportClassByNo(params);
        Map<String, Object> jiequeryMap = new HashMap<String, Object>();
        Object startTime = queryClassByNo.get(0).get("class_start_time");
        Object endTime = queryClassByNo.get(0).get("class_end_time");
        Object stationId = queryClassByNo.get(0).get("station_id");
        if(startTime == null){
            startTime = "1099-11-23 11:06:50";
        }
        jiequeryMap.put("startTime",startTime);
        jiequeryMap.put("stationId",stationId);
        jiequeryMap.put("endTime",endTime);
        List<Map<String, Object>> a= classSettlementMapper.getShiftHandover(jiequeryMap);


return a;
    }

    @Override
    public List<Map<String, Object>> getSuccession(Map<String, Object> params) {
        return  classSettlementMapper.queryReportClassByNo(params);

    }



    @Override
    public List<Map<String, Object>> employeeQueryGroupByPayTypeForOrder(Map<String, Object> params) {
        return classSettlementMapper.employeeQueryGroupByPayTypeForOrder(params);
    }



    @Override
    public List<Map<String, Object>> queryGroupByPayType(Map<String, Object> params) {
        List<Map<String, Object>> queryGroupByPayType = classSettlementMapper.queryGroupByPayType(params);
        Iterator<Map<String, Object>> iterator3 = queryGroupByPayType.iterator();
        while (iterator3.hasNext()) {
            Map<String, Object> map = iterator3.next();
            if (!map.containsKey("pay_type")) {
                iterator3.remove();
            }
        }
        Map<String, Object> allPayType = new HashMap<>();
        BigDecimal amtSum2 = BigDecimal.ZERO;
        BigDecimal refundAmtSum2 = BigDecimal.ZERO;
        BigDecimal discountAmtSum2 = BigDecimal.ZERO;
        BigDecimal bargainDiscountAmt2 = BigDecimal.ZERO;
        BigDecimal discountCouponAmtSum2 = BigDecimal.ZERO;
        BigDecimal discountCount2 = BigDecimal.ZERO;

        for (Map<String, Object> itemPayType : queryGroupByPayType) {
            Object amtObj = itemPayType.get("amt");
            double amtDouble = (amtObj != null) ? (double) amtObj : 0.0;
            BigDecimal amt = BigDecimal.valueOf(amtDouble);
            if (amt != null) {
                amtSum2 = amtSum2.add(amt);
            }

            Object refundAmtObj = itemPayType.get("refund_amt");
            double refundAmtDouble = (refundAmtObj != null) ? (double) refundAmtObj : 0.0;
            BigDecimal refundAmt = BigDecimal.valueOf(refundAmtDouble);
            if (refundAmt != null) {
                refundAmtSum2 = refundAmtSum2.add(refundAmt);
            }

            Object discountAmtObj = itemPayType.get("discount_amt");
            double discountAmtDouble = (discountAmtObj != null) ? (double) discountAmtObj : 0.0;
            BigDecimal discountAmt = BigDecimal.valueOf(discountAmtDouble);
            if (discountAmt != null) {
                discountAmtSum2 = discountAmtSum2.add(discountAmt);
            }
            Object bargain_discount_amtObj = itemPayType.get("bargain_discount_amt");
            BigDecimal bargain_discount_amt = (bargain_discount_amtObj != null) ? (BigDecimal) bargain_discount_amtObj : BigDecimal.ZERO;
            if (bargain_discount_amt != null) {
                bargainDiscountAmt2 = bargainDiscountAmt2.add(bargain_discount_amt);
            }
            Object discountCouponAmtObj = itemPayType.get("discount_coupon_amt");
            double discountCouponAmtDouble = (discountCouponAmtObj != null) ? (double) discountCouponAmtObj : 0.0;
            BigDecimal discountCouponAmt = BigDecimal.valueOf(discountCouponAmtDouble);
            if (discountCouponAmt != null) {
                discountCouponAmtSum2 = discountCouponAmtSum2.add(discountCouponAmt);
            }
            Object discountOrderCountObj = itemPayType.get("order_count");
            BigDecimal discountOrderCountint = (discountOrderCountObj != null) ? (BigDecimal) discountOrderCountObj : BigDecimal.ZERO;
            if (discountOrderCountint != null) {
                discountCount2 = discountCount2.add(discountOrderCountint);
            }
        }
        for(Map<String, Object> item :queryGroupByPayType){
            Map<String, Object> queryMap = new HashMap<String, Object>();
            queryMap.put("payType", item.get("pay_type"));
            queryMap.put("stationId", item.get("station_id"));
         String orderPayType = classSettlementMapper.queryOrderPayType(queryMap);
         if(orderPayType!=null){
             item.put("pay_type",orderPayType);
         }
        }
        allPayType.put("pay_type", "合计");
        allPayType.put("amt", amtSum2);
        allPayType.put("refund_amt", refundAmtSum2);
        allPayType.put("discount_amt", discountAmtSum2);
        allPayType.put("bargain_discount_amt", bargainDiscountAmt2);
        allPayType.put("order_count", discountCount2);
        allPayType.put("discount_coupon_amt", discountCouponAmtSum2);

        queryGroupByPayType.add(allPayType);
        return queryGroupByPayType;
    }

    @Override
    public List<Map<String, Object>> queryGroupDiscountByOilForOrder(Map<String, Object> params) {
        List<Map<String, Object>> queryGroupDiscountByOil = classSettlementMapper.queryGroupDiscountByOilForOrder(params);
        Iterator<Map<String, Object>> iteratorDiscountByOil = queryGroupDiscountByOil.iterator();
        while (iteratorDiscountByOil.hasNext()) {
            Map<String, Object> map = iteratorDiscountByOil.next();
            if (!map.containsKey("oil_name")) {
                iteratorDiscountByOil.remove();
            }
        }
        Map<String, Object> allDiscountByOil = new HashMap<>();
        BigDecimal allDiscountByOilRechargeDiscountAmtSum2 = BigDecimal.ZERO;
        BigDecimal allDiscountByOilConsumeDiscountAmtSum2 = BigDecimal.ZERO;
        BigDecimal allDiscountByOilDiscountCouponAmtSum2 = BigDecimal.ZERO;
        BigDecimal allDiscount = BigDecimal.ZERO;

        for (Map<String, Object> itemPayType : queryGroupDiscountByOil) {

            Object recharge_discount_amtObj = itemPayType.get("recharge_discount_amt");
            BigDecimal recharge_discount_amtBigDecimal = (recharge_discount_amtObj != null) ? (BigDecimal) recharge_discount_amtObj :BigDecimal.ZERO;

            if (recharge_discount_amtBigDecimal != null) {
                allDiscountByOilRechargeDiscountAmtSum2 = allDiscountByOilRechargeDiscountAmtSum2.add(recharge_discount_amtBigDecimal);
            }

            Object consume_discount_amtObj = itemPayType.get("consume_discount_amt");
            BigDecimal consume_discount_amtBigDecimal = (consume_discount_amtObj != null) ? (BigDecimal) consume_discount_amtObj :BigDecimal.ZERO;

            if (consume_discount_amtBigDecimal != null) {
                allDiscountByOilConsumeDiscountAmtSum2 = allDiscountByOilConsumeDiscountAmtSum2.add(consume_discount_amtBigDecimal);
            }
            Object all_discountObj = itemPayType.get("all_discount");
            BigDecimal all_discountBigDecimal = (all_discountObj != null) ? (BigDecimal) all_discountObj :BigDecimal.ZERO;

            if (all_discountBigDecimal != null) {
                allDiscount = allDiscount.add(all_discountBigDecimal);
            }
            Object discountCouponAmtObj = itemPayType.get("discount_coupon_amt");
            double discountCouponAmtDouble = (discountCouponAmtObj != null) ? (double) discountCouponAmtObj : 0.0;
            BigDecimal discountCouponAmt = BigDecimal.valueOf(discountCouponAmtDouble);
            if (discountCouponAmt != null) {
                allDiscountByOilDiscountCouponAmtSum2 = allDiscountByOilDiscountCouponAmtSum2.add(discountCouponAmt);
            }
        }

        allDiscountByOil.put("oil_name", "合计");
        allDiscountByOil.put("all_discount", allDiscount);
        allDiscountByOil.put("recharge_discount_amt", allDiscountByOilRechargeDiscountAmtSum2);
        allDiscountByOil.put("consume_discount_amt", allDiscountByOilConsumeDiscountAmtSum2);
        allDiscountByOil.put("discount_coupon_amt", allDiscountByOilDiscountCouponAmtSum2);

        queryGroupDiscountByOil.add(allDiscountByOil);
        return queryGroupDiscountByOil;
    }



    @Override
    public BigDecimal getAllTransaction(Map<String, Object> params,CardClassSettDto settDto) {
        List<Map<String, Object>> queryClassByNo = classSettlementMapper.queryReportClassByNo(params);
        ClassSettlementCard physicalCard = new  ClassSettlementCard();
        ClassSettlementCard fleetCard = new  ClassSettlementCard();
        if (!queryClassByNo.isEmpty()) {
            Map<String, Object> queryMap = new HashMap<String, Object>();
            Object startTime = queryClassByNo.get(0).get("class_start_time");
            Object endTime = queryClassByNo.get(0).get("class_end_time");
            Object stationId = queryClassByNo.get(0).get("station_id");

            if (startTime != null) {
                String oilstartTime = startTime.toString().substring(0, 19);
                queryMap.put("createStartTime", oilstartTime);
            }

            if (endTime != null) {
                String oilendTime = endTime.toString().substring(0, 19);
                queryMap.put("createEndTime", oilendTime);
            }

            if (stationId != null) {
                queryMap.put("stationId", stationId);
            }

            if (endTime != null && stationId != null) {
                physicalCard = classSettlementMapper.queryPhysicalCard(queryMap);

                fleetCard = classSettlementMapper.queryFleetCard(queryMap);


            }}
        BigDecimal posTransactionAmt = (settDto.getTotalAmt() != null) ?  settDto.getTotalAmt() : BigDecimal.ZERO;
        BigDecimal dkqTransactionAmt = (physicalCard.getAmt() != null) ?  physicalCard.getAmt() : BigDecimal.ZERO;
        BigDecimal cdkTransactionAmt = (fleetCard.getAmt() != null) ?  fleetCard.getAmt()  : BigDecimal.ZERO;
        return posTransactionAmt.add(dkqTransactionAmt).add(cdkTransactionAmt);
    }

    @Override
    public int getAllNewPeople(Map<String, Object> params) {
        List<Map<String, Object>> queryClassByNo = classSettlementMapper.queryReportClassByNo(params);
int allNewPeople= 0;
        if (!queryClassByNo.isEmpty()) {
            Map<String, Object> queryMap = new HashMap<String, Object>();
            Object startTime = queryClassByNo.get(0).get("class_start_time");
            Object endTime = queryClassByNo.get(0).get("class_end_time");
            Object stationId = queryClassByNo.get(0).get("station_id");

            if (startTime != null) {
                String oilstartTime = startTime.toString().substring(0, 19);
                queryMap.put("createStartTime", oilstartTime);
            }

            if (endTime != null) {
                String oilendTime = endTime.toString().substring(0, 19);
                queryMap.put("createEndTime", oilendTime);
            }

            if (stationId != null) {
                queryMap.put("stationId", stationId);
            }

            if (endTime != null && stationId != null) {

                allNewPeople=   classSettlementMapper.getAllNewPeople(queryMap);
            }}

        return allNewPeople;
    }


    @Override
    public List<Map<String, Object>> queryReportByPayType(Map<String, Object> params,CardClassSettDto settDto) {
        List<Map<String, Object>> queryGroupByPayType = classSettlementMapper.queryReportByPayType(params);
        List<Map<String, Object>> queryClassByNo = classSettlementMapper.queryReportClassByNo(params);
        ClassSettlementCard physicalCard = new  ClassSettlementCard();
        ClassSettlementCard fleetCard = new  ClassSettlementCard();
        ClassSettlementCard superPay = new  ClassSettlementCard();

        ClassSettlementCard forSaleActivity = new  ClassSettlementCard();
        int xjint = 0;
        int wxint = 0;
        if (!queryClassByNo.isEmpty()) {
            Map<String, Object> queryMap = new HashMap<String, Object>();
            Object startTime = queryClassByNo.get(0).get("class_start_time");
            Object endTime = queryClassByNo.get(0).get("class_end_time");
            Object stationId = queryClassByNo.get(0).get("station_id");

            if (startTime != null) {
                String oilstartTime = startTime.toString().substring(0, 19);
                queryMap.put("createStartTime", oilstartTime);
            }

            if (endTime != null) {
                String oilendTime = endTime.toString().substring(0, 19);
                queryMap.put("createEndTime", oilendTime);
            }

            if (stationId != null) {
                queryMap.put("stationId", stationId);
            }

            if (endTime != null && stationId != null) {
                 physicalCard = classSettlementMapper.queryPhysicalCard(queryMap);

                 fleetCard = classSettlementMapper.queryFleetCard(queryMap);

                 superPay = this.querySuperPay(queryMap);



                 forSaleActivity  =    classSettlementMapper.saleActivityAmtClass(params);
            }}


                Iterator<Map<String, Object>> iterator3 = queryGroupByPayType.iterator();
        while (iterator3.hasNext()) {
            Map<String, Object> map = iterator3.next();
            if (!map.containsKey("pay_type")) {
                iterator3.remove();
            }
            Object pay_typeObj = map.get("pay_type");
            String pay_typeStr = (pay_typeObj != null) ? (String) pay_typeObj : "其他";
            if(pay_typeStr.equals("xj")){
                xjint++;
            }
            if(pay_typeStr.equals("wx")){
                wxint++;
            }
        }
        Map<String, Object> allPayType = new HashMap<>();
        BigDecimal amtSum2 = BigDecimal.ZERO;
        BigDecimal refundAmtSum2 = BigDecimal.ZERO;
        BigDecimal discountAmtSum2 = BigDecimal.ZERO;
        BigDecimal discountCouponAmtSum2 = BigDecimal.ZERO;
        BigDecimal  orderLiters2 = BigDecimal.ZERO;
        BigDecimal transaction_amtSum = BigDecimal.ZERO;
        BigDecimal totalAmtSum2 = BigDecimal.ZERO;
        BigDecimal  commission2 = BigDecimal.ZERO;
        BigDecimal  withdrawalAmount2 = BigDecimal.ZERO;
        BigDecimal posTransactionAmt = (settDto.getTotalAmt() != null) ?  settDto.getTotalAmt() : BigDecimal.ZERO;
        BigDecimal dkqTransactionAmt = (physicalCard.getAmt() != null) ?  physicalCard.getAmt() : BigDecimal.ZERO;
        BigDecimal cdkTransactionAmt = (fleetCard.getAmt() != null) ?  fleetCard.getAmt()  : BigDecimal.ZERO;

//        秒杀金额

        BigDecimal forSaleActivityAmt = forSaleActivity != null && forSaleActivity.getAmt() != null ? forSaleActivity.getAmt() : BigDecimal.ZERO;
        BigDecimal forSaleActivityCom = forSaleActivity != null && forSaleActivity.getServiceCharge() != null ? forSaleActivity.getServiceCharge() : BigDecimal.ZERO;
        BigDecimal forSaleActivityTx = forSaleActivity != null && forSaleActivity.getWithdrawalAmount() != null ? forSaleActivity.getWithdrawalAmount() : BigDecimal.ZERO;
        BigDecimal forSaleActivityRef = forSaleActivity != null && forSaleActivity.getRefundAmount() != null ? forSaleActivity.getRefundAmount() : BigDecimal.ZERO;




        BigDecimal posimp = BigDecimal.ZERO;
        BigDecimal posmsub = BigDecimal.ZERO;
        BigDecimal poswx = BigDecimal.ZERO;
        BigDecimal poswxCommission = BigDecimal.ZERO;
        BigDecimal posrefCommission = BigDecimal.ZERO;
        BigDecimal poswxWithdrawalAmount = BigDecimal.ZERO;
        BigDecimal posref = BigDecimal.ZERO;



        List<ClassSettlementCard>  classSettlementCardL = settDto.getSettlementCardList();
//        MANUAL_ADD("imp","手动增加"),
//     *     MANUAL_SUB("msub","手动扣减"),
//     *     WX_RECHARGE("wx","微信充值"),
//     *     CONSUME("","电子卡支付"),
//     *     REFUND("ref","充值退款"),*/
       for (ClassSettlementCard classSettlementCardS:classSettlementCardL){
           if ( classSettlementCardS.getPayType().equals("imp")){
               posimp = classSettlementCardS.getAmt();
           }
           if ( classSettlementCardS.getPayType().equals("msub")){
               posmsub = classSettlementCardS.getAmt();
           }
           if ( classSettlementCardS.getPayType().equals("wx")){
               poswx = classSettlementCardS.getAmt();
               poswxCommission = classSettlementCardS.getServiceCharge();
               poswxWithdrawalAmount = classSettlementCardS.getWithdrawalAmount();
           }
           if ( classSettlementCardS.getPayType().equals("ref")){
               posref = classSettlementCardS.getAmt();
               posrefCommission = classSettlementCardS.getServiceCharge();
           }

       }
        if (xjint ==0){
            Map<String, Object> xjPayType = new HashMap<>();
            xjPayType.put("pay_type", "xj");
            xjPayType.put("amt", 0.0);
                    xjPayType.put("order_liters", 0.0);
                    xjPayType.put("refund_amt", 0.0);
            xjPayType.put("discount_amt", 0.0);
            xjPayType.put("discount_coupon_amt", 0.0);
            xjPayType.put("transaction_amt",BigDecimal.ZERO);
            queryGroupByPayType.add(xjPayType);
        }
        if (wxint ==0){
            Map<String, Object> wxPayType = new HashMap<>();
            wxPayType.put("pay_type", "wx");
            wxPayType.put("amt", 0.0);
            wxPayType.put("order_liters", 0.0);
            wxPayType.put("refund_amt", 0.0);
            wxPayType.put("discount_amt", 0.0);
            wxPayType.put("discount_coupon_amt", 0.0);
            wxPayType.put("transaction_amt",BigDecimal.ZERO);
            queryGroupByPayType.add(wxPayType);
        }
//最终做一次处理
        for (Map<String, Object> itemPayType : queryGroupByPayType) {

            Object pay_typeObj = itemPayType.get("pay_type");
            String pay_typeStr = (pay_typeObj != null) ? (String) pay_typeObj : "其他";
                Map<String, Object> OrderPayqueryMap = new HashMap<String, Object>();
                OrderPayqueryMap.put("payType", pay_typeStr);
                OrderPayqueryMap.put("stationId", params.get("stationId"));
                String orderPayType = classSettlementMapper.queryOrderPayType(OrderPayqueryMap);
                if(orderPayType!=null){
                    itemPayType.put("pay_type",orderPayType);
                }
            Object amtObj = itemPayType.get("amt");
            double amtDouble = (amtObj != null) ? (double) amtObj : 0.0;
            BigDecimal amt = BigDecimal.valueOf(amtDouble);

            Object withdrawal_amountObj = itemPayType.get("withdrawal_amount");
            BigDecimal withdrawalAmount = (withdrawal_amountObj != null) ? (BigDecimal) withdrawal_amountObj : BigDecimal.ZERO;


            Object commissionObj = itemPayType.get("commission");
            BigDecimal commissionAmount = (commissionObj != null) ? (BigDecimal) commissionObj : BigDecimal.ZERO;


            if (amt != null) {
                amtSum2 = amtSum2.add(amt);

            }
            itemPayType.put("total",amt);
            if (pay_typeStr.equals("xj")) {
                itemPayType.put("transaction_amt",dkqTransactionAmt.add(cdkTransactionAmt).add(posimp));
                itemPayType.put("total",amt.add(dkqTransactionAmt.add(cdkTransactionAmt).add(posimp)));
                itemPayType.put("commission",BigDecimal.ZERO);
                itemPayType.put("withdrawal_amount",BigDecimal.ZERO);
                itemPayType.put("sale_activity_amt",BigDecimal.ZERO);
            }else if(pay_typeStr.equals("wx")){
                itemPayType.put("transaction_amt",poswx);
                itemPayType.put("total",amt.add(poswx).add(forSaleActivityAmt));
                itemPayType.put("commission",commissionAmount.add(poswxCommission).subtract(posrefCommission).add(superPay.getServiceCharge()!=null?superPay.getServiceCharge():BigDecimal.ZERO).add(forSaleActivityCom));
                itemPayType.put("withdrawal_amount",withdrawalAmount.add(poswxWithdrawalAmount).add(superPay.getWithdrawalAmount()!=null?superPay.getWithdrawalAmount():BigDecimal.ZERO).add(forSaleActivityTx));
                itemPayType.put("sale_activity_amt",forSaleActivityAmt);
            }else{
                itemPayType.put("transaction_amt",BigDecimal.ZERO);
                itemPayType.put("sale_activity_amt",BigDecimal.ZERO);
            }
            Object commissionObj2 = itemPayType.get("commission");
            BigDecimal commissionAmount2 = (commissionObj2 != null) ? (BigDecimal) commissionObj2 : BigDecimal.ZERO;
            Object withdrawal_amountObj2 = itemPayType.get("withdrawal_amount");
            BigDecimal withdrawalAmt2 = (withdrawal_amountObj2 != null) ? (BigDecimal) withdrawal_amountObj2 : BigDecimal.ZERO;
            Object refundAmtObj = itemPayType.get("refund_amt");
            double refundAmtDouble = (refundAmtObj != null) ? (double) refundAmtObj : 0.0;
            BigDecimal refundAmt = BigDecimal.valueOf(refundAmtDouble);
            if (refundAmt != null) {
                Object paytypeObj = itemPayType.get("pay_type");
                String paytypeStr = (paytypeObj != null) ? (String) paytypeObj : "其他";
              if(paytypeStr.equals("xj")){
                  refundAmt= refundAmt.add(posmsub);
                  itemPayType.put("refund_amt",refundAmt);

              }
                if(paytypeStr.equals("wx")){
                    refundAmt= refundAmt.add(posref);
                    itemPayType.put("refund_amt",refundAmt.add(superPay.getRefundAmount()).add(forSaleActivityRef));
                    refundAmtSum2 = refundAmtSum2.add(superPay.getRefundAmount().add(forSaleActivityRef));
                }
                refundAmtSum2 = refundAmtSum2.add(refundAmt);
            }

            Object discountAmtObj = itemPayType.get("discount_amt");
            double discountAmtDouble = (discountAmtObj != null) ? (double) discountAmtObj : 0.0;
            BigDecimal discountAmt = BigDecimal.valueOf(discountAmtDouble);
            if (discountAmt != null) {
                discountAmtSum2 = discountAmtSum2.add(discountAmt);
            }

              commission2 = commission2.add(commissionAmount2);
              withdrawalAmount2 = withdrawalAmount2.add(withdrawalAmt2);

            Object discountCouponAmtObj = itemPayType.get("discount_coupon_amt");
            double discountCouponAmtDouble = (discountCouponAmtObj != null) ? (double) discountCouponAmtObj : 0.0;
            BigDecimal discountCouponAmt = BigDecimal.valueOf(discountCouponAmtDouble);
            if (discountCouponAmt != null) {
                discountCouponAmtSum2 = discountCouponAmtSum2.add(discountCouponAmt);
            }
            Object orderLitersObj = itemPayType.get("order_liters");
            double orderLitersDouble = (orderLitersObj != null) ? (double) orderLitersObj : 0.0;
            BigDecimal orderLiters = BigDecimal.valueOf(orderLitersDouble);
            if (orderLiters != null) {
                orderLiters2 = orderLiters2.add(orderLiters);
            }

            Object transaction_amtObj = itemPayType.get("transaction_amt");
            BigDecimal transaction_amt = (transaction_amtObj != null) ? (BigDecimal) transaction_amtObj : BigDecimal.ZERO;

            if (transaction_amt != null) {
                transaction_amtSum = transaction_amtSum.add(transaction_amt);
            }
            Object totalAmtObj = itemPayType.get("total");
            BigDecimal totalAmt = (totalAmtObj != null) ? (BigDecimal) totalAmtObj :BigDecimal.ZERO;

            if (totalAmt != null) {
                totalAmtSum2 = totalAmtSum2.add(totalAmt);
            }
        }


        allPayType.put("pay_type", "合计");
        allPayType.put("amt", amtSum2);
        allPayType.put("refund_amt", refundAmtSum2);
        allPayType.put("discount_amt", discountAmtSum2);
        allPayType.put("discount_coupon_amt", discountCouponAmtSum2);
        allPayType.put("order_liters", orderLiters2);
        allPayType.put("transaction_amt",transaction_amtSum);
        allPayType.put("total",totalAmtSum2);
        allPayType.put("commission",commission2);
        allPayType.put("withdrawal_amount",withdrawalAmount2);
        allPayType.put("sale_activity_amt",forSaleActivityAmt);
        queryGroupByPayType.add(allPayType);
        return queryGroupByPayType;
    }

    @Override
    public List<Map<String, Object>> queryGroupByOilGun(Map<String, Object> params) {
        List<Map<String, Object>> queryGroupByOilGun = classSettlementMapper.queryGroupByOilGun(params);
        Iterator<Map<String, Object>> iterator2 = queryGroupByOilGun.iterator();
        while (iterator2.hasNext()) {
            Map<String, Object> map = iterator2.next();
            if (!map.containsKey("oil_gun")) {
                iterator2.remove();
            }
        }
        List<Map<String, Object>> queryClassByNo = classSettlementMapper.queryClassByNo(params);
        if (!queryClassByNo.isEmpty()) {
            Map<String, Object> queryMap = new HashMap<String, Object>();
            Object startTime = queryClassByNo.get(0).get("class_start_time");
            Object endTime = queryClassByNo.get(0).get("class_end_time");
            Object stationId = queryClassByNo.get(0).get("station_id");

            if (startTime != null) {
                String oilstartTime = startTime.toString().substring(0, 19);
                queryMap.put("createStartTime", oilstartTime);
            }

            if (endTime != null) {
                String oilendTime = endTime.toString().substring(0, 19);
                queryMap.put("createEndTime", oilendTime);
            }

            if (stationId != null) {
                queryMap.put("stationId", stationId);
            }

            if (endTime != null && stationId != null) {
                List<OilEngineDto> newQueryGroupByOilGun = classSettlementMapper.classSettlementOilEngineSum(queryMap);
                List<String> oilGunListA = new ArrayList<>();
                List<String> oilGunListB = new ArrayList<>();
                if (queryGroupByOilGun.size() > 0 && newQueryGroupByOilGun.size() > 0) {
                    for (Map<String, Object> map : queryGroupByOilGun) {
                        String oilGun = map.get("oil_gun").toString();
                        oilGunListA.add(oilGun); // 存储a列表的油枪数据
                        oilGunListB.clear();
                        for (OilEngineDto oilEngineDto : newQueryGroupByOilGun) {
                            oilGunListB.add(oilEngineDto.getOilGun());
                            if (oilEngineDto != null && oilEngineDto.getOilGun() != null && oilEngineDto.getOilGun().equals(oilGun)) {
                                map.put("oil_price", oilEngineDto.getOilPrice());
                                map.put("order_liters", oilEngineDto.getOrderLiters());
                                map.put("oil_name", oilEngineDto.getOilName());
                                map.put("startFlowMeter", oilEngineDto.getStartFlowMeter());
                                map.put("endFlowMeter", oilEngineDto.getEndFlowMeter());
                                map.put("totalWordCount", 0);
                                // 获取 startFlowMeter 和 endFlowMeter 的值
                                Double startFlowMeterValue = oilEngineDto.getStartFlowMeter();
                                Double endFlowMeterValue = oilEngineDto.getEndFlowMeter();
                                // 空值检查
                                if (startFlowMeterValue != null && endFlowMeterValue != null) {
                                    // 计算差值
                                    Double diff = endFlowMeterValue - startFlowMeterValue;

                                    // 将差值放入 map 中
                                    map.put("totalWordCount", diff);
                                }
                            }
                        }
                    }
                    List<String> difference = new ArrayList<>();
                    for (String value : oilGunListB) {
                        if (!oilGunListA.contains(value)) {
                            difference.add(value);
                        }
                    }
                    for (OilEngineDto oilEngineDto : newQueryGroupByOilGun) {
                        for (String oilGun : difference) {

                            if (oilEngineDto != null && oilEngineDto.getOilGun() != null && oilEngineDto.getOilGun().equals(oilGun)) {

                                Map<String, Object> map = new HashMap<>();
                                map.put("oil_gun", oilEngineDto.getOilGun());
                                map.put("oil_price", oilEngineDto.getOilPrice());
                                map.put("order_liters", oilEngineDto.getOrderLiters());
                                map.put("oil_name", oilEngineDto.getOilName());
                                map.put("startFlowMeter", oilEngineDto.getStartFlowMeter());
                                map.put("endFlowMeter", oilEngineDto.getEndFlowMeter());
                                map.put("totalWordCount", 0);
                                // 获取 startFlowMeter 和 endFlowMeter 的值
                                Double startFlowMeterValue = oilEngineDto.getStartFlowMeter();
                                Double endFlowMeterValue = oilEngineDto.getEndFlowMeter();
                                // 空值检查
                                if (startFlowMeterValue != null && endFlowMeterValue != null) {
                                    // 计算差值
                                    Double diff = endFlowMeterValue - startFlowMeterValue;

                                    // 将差值放入 map 中
                                    map.put("totalWordCount", diff);
                                }
                                queryGroupByOilGun.add(map);
                            }
                        }
                    }

                } else if (queryGroupByOilGun.size() == 0 && newQueryGroupByOilGun.size() > 0) {
                    for (OilEngineDto oilEngineDto : newQueryGroupByOilGun) {
                        if (oilEngineDto != null && oilEngineDto.getOilGun() != null) {
                            Map<String, Object> map = new HashMap<>();
                            map.put("oil_gun", oilEngineDto.getOilGun());
                            map.put("oil_price", oilEngineDto.getOilPrice());
                            map.put("order_liters", oilEngineDto.getOrderLiters());
                            map.put("oil_name", oilEngineDto.getOilName());
                            map.put("startFlowMeter", oilEngineDto.getStartFlowMeter());
                            map.put("endFlowMeter", oilEngineDto.getEndFlowMeter());
                            map.put("totalWordCount", 0);
                            // 获取 startFlowMeter 和 endFlowMeter 的值
                            Double startFlowMeterValue = oilEngineDto.getStartFlowMeter();
                            Double endFlowMeterValue = oilEngineDto.getEndFlowMeter();
                            // 空值检查
                            if (startFlowMeterValue != null && endFlowMeterValue != null) {
                                // 计算差值
                                Double diff = endFlowMeterValue - startFlowMeterValue;

                                // 将差值放入 map 中
                                map.put("totalWordCount", diff);
                            }
                            queryGroupByOilGun.add(map);

                        }
                    }
                }
            }
        }
        return queryGroupByOilGun;
    }

    @Override
    public List<Map<String, Object>> queryReportByOilGun(Map<String, Object> params) {
        List<Map<String, Object>> queryGroupByOilGun = classSettlementMapper.queryReportByOilGun(params);
        Iterator<Map<String, Object>> iterator2 = queryGroupByOilGun.iterator();
        while (iterator2.hasNext()) {
            Map<String, Object> map = iterator2.next();
            if (!map.containsKey("oil_gun")) {
                iterator2.remove();
            }
        }
        List<Map<String, Object>> queryClassByNo = classSettlementMapper.queryReportClassByNo(params);
        if (!queryClassByNo.isEmpty()) {
            Map<String, Object> queryMap = new HashMap<String, Object>();
            Object startTime = queryClassByNo.get(0).get("class_start_time");
            Object endTime = queryClassByNo.get(0).get("class_end_time");
            Object stationId = queryClassByNo.get(0).get("station_id");

            if (startTime != null) {
                String oilstartTime = startTime.toString().substring(0, 19);
                queryMap.put("createStartTime", oilstartTime);
            }

            if (endTime != null) {
                String oilendTime = endTime.toString().substring(0, 19);
                queryMap.put("createEndTime", oilendTime);
            }

            if (stationId != null) {
                queryMap.put("stationId", stationId);
            }

            if (endTime != null && stationId != null) {
                List<OilEngineDto> newQueryGroupByOilGun = classSettlementMapper.classSettlementOilEngineSum(queryMap);
                List<String> oilGunListA = new ArrayList<>();
                List<String> oilGunListB = new ArrayList<>();
                if (queryGroupByOilGun.size() > 0 && newQueryGroupByOilGun.size() > 0) {
                    for (Map<String, Object> map : queryGroupByOilGun) {
                        String oilGun = map.get("oil_gun").toString();
                        oilGunListA.add(oilGun); // 存储a列表的油枪数据
                        oilGunListB.clear();
                        for (OilEngineDto oilEngineDto : newQueryGroupByOilGun) {
                            oilGunListB.add(oilEngineDto.getOilGun());
                            if (oilEngineDto != null && oilEngineDto.getOilGun() != null && oilEngineDto.getOilGun().equals(oilGun)) {
                                map.put("oil_price", oilEngineDto.getOilPrice());
                                map.put("order_liters", oilEngineDto.getOrderLiters());
                                map.put("oil_name", oilEngineDto.getOilName());
                                map.put("startFlowMeter", oilEngineDto.getStartFlowMeter());
                                map.put("endFlowMeter", oilEngineDto.getEndFlowMeter());
                                double dStartFlowMeterDouble = ( oilEngineDto.getStartFlowMeter() != null) ? (double)  oilEngineDto.getStartFlowMeter() : 0.0;
                                double dEndFlowMeteDouble = (oilEngineDto.getEndFlowMeter() != null) ? (double) oilEngineDto.getEndFlowMeter() : 0.0;
                                BigDecimal dStartFlowMeterAmt = BigDecimal.valueOf(dStartFlowMeterDouble);
                                BigDecimal dEndFlowMeteAmt = BigDecimal.valueOf(dEndFlowMeteDouble);
                                map.put("totalWord", dEndFlowMeteAmt.subtract(dStartFlowMeterAmt));
                            }
                        }
                    }
                    List<String> difference = new ArrayList<>();
                    for (String value : oilGunListB) {
                        if (!oilGunListA.contains(value)) {
                            difference.add(value);
                        }
                    }
                    for (OilEngineDto oilEngineDto : newQueryGroupByOilGun) {
                        for (String oilGun : difference) {

                            if (oilEngineDto != null && oilEngineDto.getOilGun() != null && oilEngineDto.getOilGun().equals(oilGun)) {

                                Map<String, Object> map = new HashMap<>();
                                map.put("oil_gun", oilEngineDto.getOilGun());
                                map.put("oil_price", oilEngineDto.getOilPrice());
                                map.put("order_liters", oilEngineDto.getOrderLiters());
                                map.put("oil_name", oilEngineDto.getOilName());
                                map.put("startFlowMeter", oilEngineDto.getStartFlowMeter());
                                map.put("endFlowMeter", oilEngineDto.getEndFlowMeter());
                                double dStartFlowMeterDouble = ( oilEngineDto.getStartFlowMeter() != null) ? (double)  oilEngineDto.getStartFlowMeter() : 0.0;
                                double dEndFlowMeteDouble = (oilEngineDto.getEndFlowMeter() != null) ? (double) oilEngineDto.getEndFlowMeter() : 0.0;
                                BigDecimal dStartFlowMeterAmt = BigDecimal.valueOf(dStartFlowMeterDouble);
                                BigDecimal dEndFlowMeteAmt = BigDecimal.valueOf(dEndFlowMeteDouble);
                                map.put("totalWord", dEndFlowMeteAmt.subtract(dStartFlowMeterAmt));
                                queryGroupByOilGun.add(map);
                            }
                        }
                    }

                } else if (queryGroupByOilGun.size() == 0 && newQueryGroupByOilGun.size() > 0) {
                    for (OilEngineDto oilEngineDto : newQueryGroupByOilGun) {
                        if (oilEngineDto != null && oilEngineDto.getOilGun() != null) {
                            Map<String, Object> map = new HashMap<>();
                            map.put("oil_gun", oilEngineDto.getOilGun());
                            map.put("oil_price", oilEngineDto.getOilPrice());
                            map.put("order_liters", oilEngineDto.getOrderLiters());
                            map.put("oil_name", oilEngineDto.getOilName());
                            map.put("startFlowMeter", oilEngineDto.getStartFlowMeter());
                            map.put("endFlowMeter", oilEngineDto.getEndFlowMeter());
                            double dStartFlowMeterDouble = ( oilEngineDto.getStartFlowMeter() != null) ? (double)  oilEngineDto.getStartFlowMeter() : 0.0;
                            double dEndFlowMeteDouble = (oilEngineDto.getEndFlowMeter() != null) ? (double) oilEngineDto.getEndFlowMeter() : 0.0;
                            BigDecimal dStartFlowMeterAmt = BigDecimal.valueOf(dStartFlowMeterDouble);
                            BigDecimal dEndFlowMeteAmt = BigDecimal.valueOf(dEndFlowMeteDouble);
                            map.put("totalWord", dEndFlowMeteAmt.subtract(dStartFlowMeterAmt));
                            queryGroupByOilGun.add(map);

                        }
                    }
                }
            }
        }
        return queryGroupByOilGun;
    }

    @Override
    public List<Map<String, Object>> queryGroupByOilGunForOrder(Map<String, Object> params) {
        return classSettlementMapper.queryGroupByOilGunForOrder(params);
    }

    @Override
    public List<Map<String, Object>> employeeQueryGroupByOilGunForOrder(Map<String, Object> params) {
        return classSettlementMapper.employeeQueryGroupByOilGunForOrder(params);
    }

    @Override
    public List<Map<String, Object>> queryGroupByActivity(Map<String, Object> params) {
        return classSettlementMapper.queryGroupByActivity(params);
    }

    @Override
    public List<Map<String, Object>> queryGroupByActivityForOrder(Map<String, Object> params) {
        return classSettlementMapper.queryGroupByActivityForOrder(params);
    }

    @Override
    public List<Map<String, Object>> selectClassSettlement4User4POSList(Map<String, Object> params) {
        List<Map<String, Object>> listDetail4User = classSettlementMapper.selectClassSettlement4User4POSList(params);
        Iterator<Map<String, Object>> iterator4 = listDetail4User.iterator();
        while (iterator4.hasNext()) {
            Map<String, Object> map = iterator4.next();
            if (!map.containsKey("oil_name")) {
                iterator4.remove();
            }
        }
        Map<String, Object> allDetail4User = new HashMap<String, Object>();
        BigDecimal orderLiterSumForUser = BigDecimal.ZERO;
        BigDecimal receivableAmtSumForUser = BigDecimal.ZERO;
        BigDecimal amtSumForUser = BigDecimal.ZERO;
        BigDecimal discountAmtSumForUser = BigDecimal.ZERO;
        BigDecimal discountCouponAmtSumForUser = BigDecimal.ZERO;
        BigDecimal refundAmtSumForUser = BigDecimal.ZERO;
        BigDecimal orderCountSumForUser = BigDecimal.ZERO;
        BigDecimal moreAmtSumForUser = BigDecimal.ZERO;

        for (Map<String, Object> itemOil : listDetail4User) {
            Object orderLiterObj = itemOil.get("order_liters");
            double orderLiterDouble = (orderLiterObj != null) ? (double) orderLiterObj : 0.0;
            BigDecimal orderLiter = BigDecimal.valueOf(orderLiterDouble);
            if (orderLiter != null) {
                orderLiterSumForUser = orderLiterSumForUser.add(orderLiter);
            }

            Object receivableAmtObj = itemOil.get("receivable_amt");
            double receivableAmtDouble = (receivableAmtObj != null) ? (double) receivableAmtObj : 0.0;
            BigDecimal receivableAmt = BigDecimal.valueOf(receivableAmtDouble);
            if (receivableAmt != null) {
                receivableAmtSumForUser = receivableAmtSumForUser.add(receivableAmt);
            }

            Object amtObj = itemOil.get("amt");
            double amtDouble = (amtObj != null) ? (double) amtObj : 0.0;
            BigDecimal amt = BigDecimal.valueOf(amtDouble);
            if (amt != null) {
                amtSumForUser = amtSumForUser.add(amt);
            }

            Object discountAmtObj = itemOil.get("discount_amt");
            double discountAmtDouble = (discountAmtObj != null) ? (double) discountAmtObj : 0.0;
            BigDecimal discountAmt = BigDecimal.valueOf(discountAmtDouble);
            if (discountAmt != null) {
                discountAmtSumForUser = discountAmtSumForUser.add(discountAmt);
            }

            Object discountCouponAmtObj = itemOil.get("discount_coupon_amt");
            double discountCouponAmtDouble = (discountCouponAmtObj != null) ? (double) discountCouponAmtObj : 0.0;
            BigDecimal discountCouponAmt = BigDecimal.valueOf(discountCouponAmtDouble);
            if (discountCouponAmt != null) {
                discountCouponAmtSumForUser = discountCouponAmtSumForUser.add(discountCouponAmt);
            }

            Object refundAmtObj = itemOil.get("refund_amt");
            double refundAmtDouble = (refundAmtObj != null) ? (double) refundAmtObj : 0.0;
            BigDecimal refundAmt = BigDecimal.valueOf(refundAmtDouble);
            if (refundAmt != null) {
                refundAmtSumForUser = refundAmtSumForUser.add(refundAmt);
            }

            Object orderCountObj = itemOil.get("order_count");
            BigDecimal orderCount = (orderCountObj != null) ? (BigDecimal) orderCountObj : BigDecimal.ZERO;
            BigDecimal orderCountBigDecimal = orderCount;
            if (orderCountBigDecimal != null) {
                orderCountSumForUser = orderCountSumForUser.add(orderCountBigDecimal);
            }

            Object moreAmtObj = itemOil.get("more_amt");
            double moreAmtDouble = (moreAmtObj != null) ? (double) moreAmtObj : 0.0;
            BigDecimal moreAmt = BigDecimal.valueOf(moreAmtDouble);
            if (moreAmt != null) {
                moreAmtSumForUser = moreAmtSumForUser.add(moreAmt);
            }
        }

        allDetail4User.put("oil_personnel_name", "合计");
        allDetail4User.put("order_liters", orderLiterSumForUser);
        allDetail4User.put("receivable_amt", receivableAmtSumForUser);
        allDetail4User.put("amt", amtSumForUser);
        allDetail4User.put("discount_amt", discountAmtSumForUser);
        allDetail4User.put("discount_coupon_amt", discountCouponAmtSumForUser);
        allDetail4User.put("refund_amt", refundAmtSumForUser);
        allDetail4User.put("order_count", orderCountSumForUser);
        allDetail4User.put("more_amt", moreAmtSumForUser);

        listDetail4User.add(allDetail4User);
        return listDetail4User;
    }

    @Override
    public List<Map<String, Object>> selectClassSettlement4User4POSListForOrder(Map<String, Object> params) {
        return classSettlementMapper.selectClassSettlement4User4POSListForOrder(params);
    }

    public List<Map<String, Object>> employeeSelectClassSettlement4User4POSListForOrder(Map<String, Object> params) {
        return classSettlementMapper.employeeSelectClassSettlement4User4POSListForOrder(params);
    }

    @Override
    //统计班结信息，按照支付方式进行集合
    public List<ClassSettlement> queryClassSettlementPayType(Map<String, Object> params) {
        List<ClassSettlement> queryGroupByPayType = classSettlementMapper.queryClassSettlementPayType(params);
        Iterator<ClassSettlement> iterator3 = queryGroupByPayType.iterator();
        while (iterator3.hasNext()) {
            ClassSettlement map = iterator3.next();
            if (map.getPayType()==null) {
                iterator3.remove();
            }
        }

        for(ClassSettlement item :queryGroupByPayType){
            Map<String, Object> queryMap = new HashMap<String, Object>();
            queryMap.put("payType", item.getPayType());
            queryMap.put("stationId", item.getStationId());
            String orderPayType = classSettlementMapper.queryOrderPayType(queryMap);
            if(orderPayType!=null){
                item.setPayType(orderPayType);
            }
        }
        return queryGroupByPayType;
    }

    @Override
    //统计班结信息，按照油品名称进行集合
    public List<ClassSettlement> queryClassSettlementOil(Map<String, Object> params) {
        return classSettlementMapper.queryClassSettlementOil(params);
    }

    public String queryClassDayOrNight(Long stationId, Date createEndTime) {
        Map<String, Object> params = new HashMap<>();
        params.put("stationId", stationId);
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        String formattedStartTime = format.format(createEndTime);
        params.put("createEndTime", formattedStartTime);
        return classSettlementMapper.queryClassDayOrNight(params);
    }

    public int getDayOrNightCount(Map<String, Object> params) {

        return classSettlementMapper.getDayOrNightCount(params);
    }




    //统计班结信息，按照加油员分类
    public List<ClassSettlement> selectClassSettlementSummaryUser(Map<String, Object> params) {
        return classSettlementMapper.selectClassSettlementSummaryUser(params);
    }


    public void updateClassSettlementPrintStatus(String classNo) {
        classSettlementMapper.updateClassSettlementPrintStatus(classNo);
        classSettlementMapper.updateClassSettlementStaffPrintStatus(classNo);
    }

}
